Xcode/ios5 - Long Touch手势调用两次

Dav*_*nte 2 xcode selected uisegmentedcontrol gestures

我有一个分段控件,允许短手势和长手势.短手势识别很好.长手势方法被调用两次.我为什么要挠头.

这是构建颜色工具栏的代码的一部分:

UILongPressGestureRecognizer* longPressGestureRec =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPressGestureRec.minimumPressDuration = 1.5;
    //longPressGestureRec.cancelsTouchesInView = NO;
    [colorControl addGestureRecognizer:longPressGestureRec];
Run Code Online (Sandbox Code Playgroud)

这是longPress方法的一部分:

-(void) longPress:(id)sender {
    NSLog(@"%s", __FUNCTION__);     
    switch (colorIndex) {
        case 0:
            [self showMoreWhiteColors:(id)sender];
            break;

        case 1:
            [self showMoreRedColors:(id)sender];
            break;
Run Code Online (Sandbox Code Playgroud)

通过查看日志,我可以看到每次按住按钮时都会调用longPress方法两次.

我有什么想法,错过了,没有做....

Phi*_*lip 7

我只是检查状态是否只是UIGestureRecognizerStateBegan,否则在执行我想要的代码之前返回.所以:

-(void) longPressGesture:(UIGestureRecognizer*)gesture
{
    if ( gesture.state != UIGestureRecognizerStateBegan )
       return; // discard everything else

   // do something in response to long gesture
}
Run Code Online (Sandbox Code Playgroud)