斯威夫特:当放大镜接近视图边界时放大

时间:2015-12-02 14:40:09

标签: ios swift uiview uiviewcontroller

您可以从以下屏幕截图中看到

enter image description here

如果我对视图的边界过分接近放大(这是一个包含UIView的{​​{1}},那么我会得到令人讨厌和奇怪的效果。所以我试图将整个UIImageView设置为放大和放大的视图 实际上,这种效果会消失,但看看会发生什么:

enter image description here

似乎放大镜圆圈中显示的放大区域不再对应于我正在点击的区域(使用鼠标光标)。我希望将放大镜固定在屏幕区域中,我将根据用户点击的位置进行计算;目前我把它放在中心位置。这是我的放大镜类

UIViewController.view

其中import UIKit public class MagnifyingGlassView: UIView { var viewToMagnify: UIView? var scale: CGFloat = 2.0 var zoomedPoint:CGPoint? var size: CGFloat = 200.0 { didSet { let c = center frame = CGRectMake(c.x - size / 2, c.y - size / 2, size, size) } } var outlineColor: UIColor? { didSet { layer.borderColor = outlineColor?.CGColor } } var outlineWidth: CGFloat? { didSet { layer.borderWidth = outlineWidth ?? 0.0 } } var touchPoint: CGPoint = CGPointZero { didSet { self.center = touchPoint } } required public init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! commonInit() } required public override init(frame: CGRect) { super.init(frame: frame) commonInit() } public init() { super.init(frame: CGRectMake(0, 0, size, size)) commonInit() } public override func drawRect(rect: CGRect) { guard let context = UIGraphicsGetCurrentContext() else { return } CGContextTranslateCTM(context, frame.size.width / 2, frame.size.height / 2) CGContextScaleCTM(context, scale, scale) CGContextTranslateCTM(context, -zoomedPoint!.x, -zoomedPoint!.y) hidden = true viewToMagnify?.layer.renderInContext(context) /* color of axes */ CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0) /* line width of axes */ CGContextSetLineWidth(context, 0.5) /* draw vertical axis inside magnifier */ CGContextMoveToPoint(context, self.zoomedPoint!.x , self.zoomedPoint!.y - (self.frame.size.height / 2)) CGContextAddLineToPoint(context, self.zoomedPoint!.x, self.zoomedPoint!.y + (self.frame.size.height / 2)) /* draw horizontal axis inside magnifier */ CGContextMoveToPoint(context, self.zoomedPoint!.x - (self.frame.size.width / 2), self.zoomedPoint!.y) CGContextAddLineToPoint(context, self.zoomedPoint!.x + (self.frame.size.width / 2), self.zoomedPoint!.y) CGContextStrokePath(context) hidden = false } private func commonInit() { layer.cornerRadius = frame.size.width / 2 layer.masksToBounds = true } } 是屏幕中用户点击的点。你知道怎么纠正这个吗?

0 个答案:

没有答案