當你使用longPress gesture recognizer 時,你可能會發現調用了多次。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
longPress.delegate = self;
[self.viewaddGestureRecognizer:longPress];
其實時因為響應不同的狀態。所以,要在你的方法里加上狀態的判斷。可以看到,開始響應,結束響應,如果你不判斷的話,都會調用你的方法。
-(void)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
[self yourMethod];
}
}