UIRotationGestureRecognizer activates Swift several times

When used, UIRotationGestureRecognizerrotation is recognized, but it is triggered several 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

    })
}
+4
source share
2 answers

Check the status of the UIRotationGestureRecognizer.

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

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

I expect that you just get the state Beganand End.

+2
source

The UIRotationGestureRecognizer will trigger its assigned action as many times as it receives a notification that a multiple-finger rotational gesture has been performed. Expected that

lol()

will be called several times.

, , . UIView.animateWithDuration( ) .rotation UIRotationGestureRecognizer, , .

:

let transform = CGAffineTransformRotate(self.block.transform, rotation.rotation)
self.block.transform = transform

lol().

+1

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


All Articles