Notability和其他笔记应用程序具有此"缩放框"功能,您可以在底部的放大框中绘制.用户还可以拖动顶部的框以更改底部放大的内容.我已经尝试了我能想到的所有内容,以便在我的应用中添加此功能.我在两个视图中添加了相同的文档,但后来我遇到了很多内存问题,我复制了文件,但又重复了内存问题.有谁知道一个简单的方法来做到这一点?无论如何我可以只有一个放大另一个视图的视图?
创建一个新的 Cocoa Touch 类(可以将其命名为 MagnifyView)并将其设置为 UIView 的子类 在您的类中添加以下代码:
var viewToMagnify: UIView!
var touchPoint: CGPoint!
override init(frame: CGRect)
{
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit()
{
// Set border color, border width and corner radius of the magnify view
self.layer.borderColor = UIColor.lightGray.cgColor
self.layer.borderWidth = 3
self.layer.cornerRadius = 50
self.layer.masksToBounds = true
}
func setTouchPoint(pt: CGPoint)
{
touchPoint = pt
self.center = CGPoint(x: pt.x, y: pt.y - 100)
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context!.translateBy(x: 1 * (self.frame.size.width * 0.5), y: 1 * (self.frame.size.height * 0.5))
context!.scaleBy(x: 1.5, y: 1.5) // 1.5 is the zoom scale
context!.translateBy(x: -1 * (touchPoint.x), y: -1 * (touchPoint.y))
self.viewToMagnify.layer.render(in: context!)
}
Run Code Online (Sandbox Code Playgroud)
要使用它,请在您想要具有放大效果的视图控制器中实现touchesBegan、touchesMoved和touchesEnd函数。
具体方法如下:
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
let point = touches.first?.location(in: self.view)
if magnifyView == nil
{
magnifyView = MagnifyView.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
magnifyView.viewToMagnify = self.view
magnifyView.setTouchPoint(pt: point!)
self.view.addSubview(magnifyView)
}
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
if magnifyView != nil
{
magnifyView.removeFromSuperview()
magnifyView = nil
}
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
let point = touches.first?.location(in: self.view)
magnifyView.setTouchPoint(pt: point!)
magnifyView.setNeedsDisplay()
}
Run Code Online (Sandbox Code Playgroud)
原始来源在这里