美文网首页
50行代码搞定放大镜

50行代码搞定放大镜

作者: 晚雪浓情 | 来源:发表于2017-02-09 19:02 被阅读56次

    虽然是2012年就有的功能了。。但是还是用swift3.0更新一下。一如既往的简单,不到50行就能搞定。

    食用方法: 在控制器中添加如下代码即可

        fileprivate lazy var magnifier = MagnifierView()
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
        {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {[unowned self] in
                self.magnifier.makeKeyAndVisible()
            }
            magnifier.viewToMagnify = view.window
            //如果在View里使用,请去掉"view."即可
            magnifier.pointToMagnify = touches.first?.location(in: view.window)
        }
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
        {
            if !magnifier.isHidden
            {
                magnifier.pointToMagnify = touches.first?.location(in: view.window)
            }
        }
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            magnifier.isHidden = true
        }
        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            magnifier.isHidden = true
        }
    

    放大镜代码⬇️

    class MagnifierView: UIWindow
    {
        fileprivate static let magnifierWH:CGFloat = 120
        
        var viewToMagnify:UIView!
        
        var pointToMagnify:CGPoint!
        {
            didSet
            {
                center = CGPoint(x: pointToMagnify.x, y: pointToMagnify.y)
                contentLayer.setNeedsDisplay()
            }
        }
    
        fileprivate lazy var contentLayer:CALayer = {
        
            let layer = CALayer()
            layer.delegate = self
            layer.frame = self.bounds
            layer.contentsScale = UIScreen.main.scale
            
            return layer
        }()
    
        private override init(frame: CGRect)
        {
            super.init(frame: frame)
            
            backgroundColor = .clear
            layer.borderWidth = 1
            layer.borderColor = UIColor.lightGray.cgColor
            layer.cornerRadius = MagnifierView.magnifierWH * 0.5
            layer.masksToBounds = true
            windowLevel = UIWindowLevelAlert
            
            layer.addSublayer(contentLayer)
        }
        
        convenience init()
        {
            self.init(frame: CGRect(x: 0, y: 0, width: MagnifierView.magnifierWH, height: MagnifierView.magnifierWH))
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func draw(_ layer: CALayer, in ctx: CGContext) {
    
            ctx.translateBy(x: frame.size.width * 0.5, y: frame.size.height * 0.5)
            ctx.scaleBy(x: 1.2, y: 1.2)
            ctx.translateBy(x: -pointToMagnify.x, y: -pointToMagnify.y)
            
            viewToMagnify.layer.render(in: ctx)
        }
    }
    

    相关文章

      网友评论

          本文标题:50行代码搞定放大镜

          本文链接:https://www.haomeiwen.com/subject/ptjwittx.html