Notability和其他笔记应用程序都有这个“缩放框”功能,你可以在底部的放大框中绘制。用户还可以拖动顶部的框来更改想要在底部放大的内容。我已经尝试了几乎所有我能想到的在我的应用程序中添加这个功能的方法。我已经在两个视图中添加了相同的文档,但是我遇到了许多内存问题,我复制了文件,但又出现了内存问题。有没有人知道一种简单的方法?有没有什么办法,我可以只有一个视图,是另一个视图的放大?

发布于 2018-09-19 05:07:32
创建一个新的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!)
}要使用它,在视图控制器中实现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
https://stackoverflow.com/questions/52393900
复制相似问题