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!) }
要使用它,在View Controller中实现想要具有放大效果的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() }
原始来源here