UIRotationGestureRecognizer

UIRotationGestureRecognizer

使用UIRotationGestureRecognizer时,可以识别旋转,但会多次触发动作。这是我的代码:

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

    })
}

最佳答案

查看UIRotationGestureRecognizer的状态。

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

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

我希望您刚收到BeganEnd状态。

09-11 11:24