UILongPressGestureRecognizer的selector多次调用解决方法

mac2022-06-30  23

当你使用longPress gesture recognizer 时,你可能会发现调用了多次。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; longPress.delegate = self; [self.view addGestureRecognizer:longPress];

其实时因为响应不同的状态。所以,要在你的方法里加上状态的判断。可以看到,开始响应,结束响应,如果你不判断的话,都会调用你的方法。

- (void)longPress:(UILongPressGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan) { [self yourMethod]; } }

转载于:https://www.cnblogs.com/LeeYZ/p/5037722.html

最新回复(0)