Abhi V Abhi V - 4 months ago 10
iOS Question

UIRotationGestureRecognizer activates multiple times Swift

When using

UIRotationGestureRecognizer
the rotation is recognized but it triggers the action multiple times. Here is my code:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.lol))
self.view.addGestureRecognizer(rotation)
}

func lol() {
print ("hi")
UIView.animateWithDuration(5.0, animations: {
let currTransform = self.view.transform
let newTransform = CGAffineTransformConcat(currTransform, CGAffineTransformMakeRotation(CGFloat(M_PI)))
self.view.transform = newTransform

})
}

Answer

Check out UIRotationGestureRecognizer's state.

let rotation = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.lol(_:)))

func lol(sender: UIRotationGestureRecognizer) {
    print(sender.state)

I expect you're just receiving Began and End states.