Swift 3:我如何用捏合手势缩放和旋转UIImageView?

12

我很难在网上找到教程以及已经回答过的问题(我尝试过它们,但它们似乎不起作用)。我有一个UIImageView,它位于我的视图中心。我目前能够点击并拖动此视图到屏幕上任何位置。我想能够通过捏合来缩放和旋转此视图。我如何实现这一点?我已经尝试了下面的旋转代码,但似乎不起作用?任何帮助都将是巨大的帮助,并标记为答案。谢谢大家。

    import UIKit

class DraggableImage: UIImageView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.backgroundColor = .blue

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.backgroundColor = .green

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: superview)
            center = CGPoint(x: position.x, y: position.y)
        }
    }

}

class CVController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotateAction(sender:)))
        firstImageView.addGestureRecognizer(rotateGesture)

        setupViews()
    }

    func rotateAction(sender: UIRotationGestureRecognizer) {
        let rotatePoint = sender.location(in: view)
        let firstImageView = view.hitTest(rotatePoint, with: nil)
        firstImageView?.transform = (firstImageView?.transform.rotated(by: sender.rotation))!
        sender.rotation = 0
    }

    let firstImageView: DraggableImage = {
        let iv = DraggableImage()
        iv.backgroundColor = .red
        iv.isUserInteractionEnabled = true
        return iv
    }()

    func setupViews() {
        view.addSubview(firstImageView)

        let firstImageWidth: CGFloat = 50
        let firstImageHeight: CGFloat = 50

        firstImageView.frame = CGRect(x: (view.frame.width / 2) - firstImageWidth / 2, y: (view.frame.height / 2) - firstImageWidth / 2, width: firstImageWidth, height: firstImageHeight)
    }

}

你的旋转代码似乎是正确的。你尝试在调试器中设置断点,看看是否识别了旋转手势并执行了旋转代码吗? - Fahim
与您的问题无关,但您可以直接传递位置 center = position - Leo Dabus
顺便提一下,您可以将手势识别器添加到主视图中,在手势选择器方法中添加 firstImageView.transform = CGAffineTransform(rotationAngle: gesture.rotation) - Leo Dabus
1个回答

13

你的代码存在一些问题。首先,需要将 UIGestureRecognizerDelegate 添加到你的视图控制器中,并将它设置为手势识别器的代理。其次,你还需要实现shouldRecognizeSimultaneously方法并返回true。其次,在应用缩放时,需要在捏合手势开始时保存变换矩阵,并在其基础上应用缩放:

class DraggableImageView: UIImageView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        backgroundColor = .blue
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        backgroundColor = .green
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let position = touches.first?.location(in: superview){
            center = position
        }
    }
}

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    
    var identity = CGAffineTransform.identity
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setupViews()
        
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(scale))
        let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotate))
        
        pinchGesture.delegate = self
        rotationGesture.delegate = self

        view.addGestureRecognizer(pinchGesture)
        view.addGestureRecognizer(rotationGesture)
    }
    let firstImageView: DraggableImageView = {
        let iv = DraggableImageView()
        iv.backgroundColor = .red
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    func setupViews() {
        view.addSubview(firstImageView)
        let firstImageWidth: CGFloat = 50
        let firstImageHeight: CGFloat = 50
        firstImageView.frame = CGRect(x: view.frame.midX - firstImageWidth / 2, y: view.frame.midY - firstImageWidth / 2, width: firstImageWidth, height: firstImageHeight)
    }
    @objc func scale(_ gesture: UIPinchGestureRecognizer) {
        switch gesture.state {
        case .began:
            identity = firstImageView.transform
        case .changed,.ended:
            firstImageView.transform = identity.scaledBy(x: gesture.scale, y: gesture.scale)
        case .cancelled:
            break
        default:
            break
        }
    }
    @objc func rotate(_ gesture: UIRotationGestureRecognizer) {
        firstImageView.transform = firstImageView.transform.rotated(by: gesture.rotation)
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接