Swift 3: How to pinch a scale and rotate a UIImageView?

I am really trying to find textbooks online, and have already answered questions (I tried them and they don't seem to work). I have a UIImageView, which I have at the center of my gaze. Currently, I can use and drag it wherever I want on the screen. I want to be able to scale and rotate this view. How do I achieve this? I tried the code to rotate below, but it doesn't seem to work? Any help will be of great help and will be marked as an answer. Thanks guys.

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) } } 
+8
source share
1 answer

You have some problems in your code. First you need to add the UIGestureRecognizerDelegate to your view controller and make it a gesture recognizer delegate. and add the shouldRecognizeSimually method and return true. Secondly, when applying scale, you need to save the transformation when the pinch starts, and apply the scale at the top:

 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 } } 
+7
source

Source: https://habr.com/ru/post/1265804/


All Articles