Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode/ios5-长触摸手势调用两次_Xcode_Uisegmentedcontrol_Selected_Gestures - Fatal编程技术网

Xcode/ios5-长触摸手势调用两次

Xcode/ios5-长触摸手势调用两次,xcode,uisegmentedcontrol,selected,gestures,Xcode,Uisegmentedcontrol,Selected,Gestures,我有一个分段控件,允许短手势和长手势。短手势识别很好。长手势方法被调用了两次。我不明白为什么 这是构建颜色工具栏代码的一部分: UILongPressGestureRecognizer* longPressGestureRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; longPressGestureRec.minimumPressDurat

我有一个分段控件,允许短手势和长手势。短手势识别很好。长手势方法被调用了两次。我不明白为什么

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

UILongPressGestureRecognizer* longPressGestureRec =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPressGestureRec.minimumPressDuration = 1.5;
    //longPressGestureRec.cancelsTouchesInView = NO;
    [colorControl addGestureRecognizer:longPressGestureRec];
这是longPress方法的一部分:

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

        case 1:
            [self showMoreRedColors:(id)sender];
            break;
通过查看日志,我可以看到每次按住按钮时都会调用两次longPress方法


知道我做错了什么、遗漏了什么、没有做什么吗?

我只是检查状态是否不是UIgestureRecognitizerStateStateStart,然后在执行我想要的代码之前返回其他状态。因此:

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

   // do something in response to long gesture
}

或者你可以这样做

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
      switch(gesture.state){
       case UIGestureRecognizerStateBegan:

            // Do your stuff here.
            NSLog(@"State Began");
            break;
       case UIGestureRecognizerStateChanged:
            NSLog(@"State changed");
            break;
       case UIGestureRecognizerStateEnd:
            NSLog(@"State End");
            break;
       default:
            break;
      }
}

}

回答:你好,菲利普。谢谢你的回复。我看到了那个帖子。这里似乎有几个相互矛盾的答案。你有什么建议吗?谢谢!我会把这个留到下一个版本+1.
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == gesture.state) {
    // Called on start of gesture, do work here
}

if(UIGestureRecognizerStateChanged == gesture.state) {
    // Do repeated work here (repeats continuously) while finger is down
}

if(UIGestureRecognizerStateEnded == gesture.state) {
    // Do end work here when finger is lifted
}