Swift:PinchGesture,缩放后,我的视线超出了其他视线

时间:2018-08-07 14:48:44

标签: swift gesture

我创建了一个允许您放大图像视图的功能,但是当我放大图像时,关闭按钮不再起作用。看起来像是缩放,图像视图安装在我的UIButton上方。

在故事板中,UIButton在imageView上方。

怎么办?这是我的代码:

   class DisplayImageTeamController: UIViewController {

    @IBOutlet weak var imageTeam: UIImageView!

    var team: Team!


    override func viewDidLoad() {
        super.viewDidLoad()

        guard team != nil else {
            return
        }

        loadImageFromURL(url: team.imageUrl, imageView: self.imageTeam)
        pinchGestureInitializer()
    }

    @IBAction func closeButton(_ sender: UIButton) {
        dismiss(animated: true)
    }
}

//MARK Pinch Gesture
extension DisplayImageTeamController {
    func pinchGestureInitializer() {
        imageTeam.isUserInteractionEnabled = true

        //Create pinch gesture
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinch(sender:)))

        //Add pinch gesture to imageview
        imageTeam.addGestureRecognizer(pinchGesture)
    }

    @objc func pinch(sender: UIPinchGestureRecognizer) {
        sender.view?.transform = (sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale))!
        sender.scale = 1.0

        let transformedSize = sender.view?.frame.size.width
        let originalSize = sender.view?.bounds.size.width

        if sender.state == .ended {
            if transformedSize! < originalSize! {
                UIView.animate(withDuration: 0.2) {
                    sender.view?.transform = CGAffineTransform.identity //We return the view to its original position
                }
            }
        }
    }
}

感谢您的帮助

0 个答案:

没有答案