Swift 3: How do I pinch to scale and rotate UIImageView?

后端 未结 1 605
悲哀的现实
悲哀的现实 2021-01-02 09:27

I am really struggling to find tutorials online as well as already answered questions (I have tried them and they don\'t seem to work). I have a UIImageView that I have in t

相关标签:
1条回答
  • 2021-01-02 10:10

    You have a some problems in your code. First you need to add the UIGestureRecognizerDelegate to your view controller and make it your gesture recognizer delegate. and add the shouldRecognizeSimultaneously method and return true. Second when applying the scale you need to save the transform when the pinch begins and apply the scale in top of it:

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题