快速放大视图问题-图像到越野车

时间:2019-05-06 09:14:04

标签: ios swift view magnify loupe

我正在尝试为UIImageView做放大镜。 想法很简单:我有UIView的子类MagnifyingView。该视图是静态放置在屏幕底部的,当用户用手指“绘画”时出现。我没有套索,橡皮擦和还原工具之类的工具,用户希望对绘画方式有更多的控制权

这是MagnifyingView代码:


import UIKit

fileprivate extension UIView {

    fileprivate var _width_:CGFloat {
        get { return self.frame.size.width }
    }

    fileprivate var _height_:CGFloat {
        get { return self.frame.size.height }
    }
}

public enum MagnifyingViewShap {
    case Round
    case Square
    case Custom
}

public class MagnifyingView: UIView {

    public var magnifyView:UIView!

    public var scaleFactor:CGFloat = 2.0 {
        didSet {
            self.setNeedsDisplay()
        }
    }

    public var magnifyShap:MagnifyingViewShap = .Square {
        willSet {
            UIView.animate(withDuration: 0.25, animations: {
                if newValue == .Round {
                    self.layer.masksToBounds = true
                    self.layer.cornerRadius = min(self._width_, self._height_) / 2.0
                } else {
                    self.layer.cornerRadius = 0
                }
            }) { _ in
                if newValue == .Square {
                    self.layer.masksToBounds = false
                }
            }
        }
    }

    public var exceptOutSide:Bool = true {
        didSet {
            self.setNeedsDisplay()
        }
    }

    public var touchLocation:CGPoint? {
        didSet {
            self.setNeedsDisplay()
        }
    }

    override public func draw(_ rect: CGRect) {
        super.draw(rect)

        if let location = self.touchLocation {
            var trsx = -1 * (location.x + self._width_ / self.scaleFactor / 2.0)
            var trsy = -1 * (location.y + self._height_ / self.scaleFactor / 2.0)

            if self.exceptOutSide {
                if location.x <= self._width_ / self.scaleFactor / 2.0 {

                    trsx = -1 * self._width_ / self.scaleFactor
                } else if location.x >= self.magnifyView._width_ - self._width_ / self.scaleFactor / 2.0 {

                    trsx = -1 * self.magnifyView._width_
                }

                if location.y <= self._height_ / self.scaleFactor / 2.0 {

                    trsy = -1 * self._height_ / self.scaleFactor
                } else if location.y >= self.magnifyView._height_ - self._height_ / self.scaleFactor / 2.0 {

                    trsy = -1 * self.magnifyView._height_
                }
            }

            self.isHidden = true

            if let context = UIGraphicsGetCurrentContext() {
                context.translateBy(x: self._width_, y: self._height_)
                context.scaleBy(x: self.scaleFactor, y: self.scaleFactor)
                context.translateBy(x: trsx, y: trsy)
                self.magnifyView.layer.render(in: context)
            }

            self.isHidden = false
        }
    }
}

这是我的使用方式:

@IBOutlet weak var loupe: MagnifyingView!

// then init:
loupe.magnifyView = lassoImageView
loupe.scaleFactor = 2

// this is magnifying func I'm calling in touchesBegan and touchesMoved methods
func magnifyingTouch(position: CGPoint) {
    self.loupe.touchLocation = position
}

它可以工作,但是非常容易出故障,第二次点击的效果就好象被缓存了

也许有人知道如何解决此问题?

0 个答案:

没有答案