UIRotationGestureRecognizer多次激活Swift

时间:2016-08-07 09:07:35

标签: ios swift xcode uigesturerecognizer

使用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

    })
}

2 个答案:

答案 0 :(得分:2)

查看UIRotationGestureRecognizer的状态。

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

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

我希望您刚收到BeganEnd个州。

答案 1 :(得分:1)

UIRotationGestureRecognizer会在收到已执行旋转多指触摸手势的通知时多次触发其指定的操作。预计

lol()

将被多次调用。

我假设你想要旋转视图,因为用户在视图对象上使用了捏合手势。在这种情况下,您应该监听UIRotationGestureRecognizer实例的 .rotation 属性,而不是使用UIView.animateWithDuration(其中您必须手动设置持续时间),该属性将返回最新的旋转值手势识别器,用弧度表示。

然后,您可以使用此属性设置视图的旋转:

let transform = CGAffineTransformRotate(self.block.transform, rotation.rotation)
self.block.transform = transform
在你的lol()函数中。

相关问题