2013年11月1日 星期五

UILongPressGestureRecognizer 被呼叫兩次的解決方法

荒廢已久的《AMIGO Controller》一直沒有再次提起,似乎不太想面對那複雜的邏輯(新動作編輯系統)。今日勉強地重拾工作,找到長按 MotionButton 時會出現兩次 UILongPressGestureRecognizer 的錯誤,於是著手解決。以下是原來的代碼:
- (void)handleLongPress:(id)sender  {
 [self updateTouches:CGPointMake(-1, -1)];
 if ([_delegate respondsToSelector:@selector(motionButtonLongPress:)] == NO)  {return;}
 [_delegate motionButtonLongPress:self];
}
解決方法是在最初檢查一下狀態,要是 UIGestureRecognizerStateBegan 才做:
- (void)handleLongPress:(id)sender  {
 UILongPressGestureRecognizer *recognizer = (UILongPressGestureRecognizer *)sender;
 if (recognizer.state != UIGestureRecognizerStateBegan)  {return;}

 [self updateTouches:CGPointMake(-1, -1)];
 if ([_delegate respondsToSelector:@selector(motionButtonLongPress:)] == NO)  {return;}
 [_delegate motionButtonLongPress:self];
}

沒有留言: