UIRotationGestureRecognizer
suggest changeTwo fingers rotating around a center can be listened for with the UIRotationGestureRecognizer. This is generally used for rotating a view. This sample is Swift 2. It replaces rotateView’s transform with a rotation from the identity, using the gesture’s rotation since the gesture began. rotateView and label are the sample’s own views. In Swift 3 the state is .changed and the transform is CGAffineTransform(rotationAngle:).
Swift
override func viewDidLoad() {
super.viewDidLoad()
// Rotate
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))
rotateView.addGestureRecognizer(rotateGesture)
}
// Rotate action
func handleRotate(gesture: UIRotationGestureRecognizer) {
label.text = "Rotate recognized"
if gesture.state == UIGestureRecognizerState.Changed {
let transform = CGAffineTransformMakeRotation(gesture.rotation)
rotateView.transform = transform
}
}
Notes
- A sample project can be found here.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents