UILongPressGestureRecognizer停止手柄而不停止触摸

时间:2012-03-13 16:34:51

标签: iphone ios uigesturerecognizer

我正在使用UILongPressGestureRecognizer类来处理是否正在选择一个项目。

逻辑如下:用户在1秒钟内按下一个项目(UIView子类)。一旦检测到手势,该项目就会突出显示并可移动。

用户必须在屏幕上移动此项目而不要停止触摸它。

我面临的问题是手势识别阴影触摸开始/移动/结束项目类安排移动所必需的。

我尝试删除检测到的手势并选择了该项目。但仍然会向手势句柄发送消息而不是调用触摸方法。

任何人都知道如何停止“聆听”手势识别器而不离开屏幕的手指?

感谢。

这里是代码:

-(void)addGestures
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self 
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = iItemLongPressTime;
    [self addGestureRecognizer:longPress];
    [longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {

        NSLog(@"Long press Ended");
    }
    else {
        if (self.isSelected) return;

        if ([delegate respondsToSelector:@selector(singleTouch:)])
            [delegate singleTouch:self];

        [self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]];

        NSLog(@"Long press detected.");
    }
}

正如您在else分支中看到的,委托调用允许所有过程将此项目标记为已选中,并在删除识别器之后。

我缺少什么?

- 编辑 -

完成!这有效:

#pragma mark Gesture Functions
-(void)addGestures
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self 
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = iItemLongPressTime;
    [self addGestureRecognizer:longPress];
    [longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {

        NSLog(@"Long press Ended");
    }
    else {
        NSLog(@"Long press detected.");

        if (self.isSelected) return;

        if ([delegate respondsToSelector:@selector(singleTouch:)])
            [delegate singleTouch:self];

        [sender removeTarget:self action:@selector(handleLongPress:)];
        sender.enabled = NO;
        [self removeGestureRecognizer:sender];



    }
}

问候!

2 个答案:

答案 0 :(得分:4)

自定义UIView类是否有自己的触摸处理代码?如果没有,一个简单的解决方案是将allowableMovement的{​​{1}}属性设置为UILongPressGestureRecognizer或一些大数字,并使用手势更新回调来拖动自定义视图。您可以使用superview上的CGFLOAT_MAX方法获取位移,并将其位置与识别器开始时的位置进行比较。

答案 1 :(得分:1)

我脑子里有两种解决方案。

  1. 为了动画uiview,请写一个继承自UIView类的新类,并实现触摸委托,而不是编写Gustures来处理动画(如果触摸委托没有在当前类中触发)。
  2. 2.我在触发一次后成功删除了UILongPressGestureRecognizer。

    如果您有任何疑问,请参阅以下代码.ask我

    我已完成的步骤

    主视图加载时,我在主视图中添加了一个UIView作为“myView”。

    我已将标签提供给myView(您可以提供1,2,3等)以区分点击视图和主视图子视图。

    将UILongPressGestureRecognizer手势分配给myView并将目标指定为“moveMe”方法。

    当用户长按myView时,将触发“moveMe”方法。

    然后我用条件Tag == 1

    迭代mainView子视图

    我已从子视图中删除了UILongPressGestureRecognizer。我们可以知道Tagged 1主视图子视图是myView。

    所以NSLog(@“手势删除”);和NSLog(@“moveMe”);将仅在一次登录控制台。

    NSLog(@“touchesBegan”);将首先触发,而不是触发“moveMe”方法。

    然后NSLog(@“touchesBegan”);删除手势后将始终触发。 “moveMe”方法永远不会触发。

    <强>代码

        - (void)viewDidLoad {    
            //Adding to UIView to main view when application is loading.
             UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];        
             myView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
              myView.tag = 1; //adding a tag to identify it.
            //Adding Long Press Gesture to the UIView.
            UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveMe:)];
            [myView addGestureRecognizer:myGesture];
            [myGesture release];
            myGesture = nil;   
           [self.view addSubview:myView];   
           [myView release];
            myView = nil;    
            [super viewDidLoad];
        }    
    
        //Method to trigger when user pressed long on the added UIView.
    
     -(void)moveMe:(id)sender
     { 
          for (UIView *subViews in [self.view subviews]) 
          { 
                if (subViews.tag == 1) { 
                     [subViews removeGestureRecognizer:sender];
                     NSLog(@"gesture removed");
                 }    
             }    
             NSLog(@"moveMe");
        }    
     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     {
         NSLog(@"touchesBegan");
     }
    

    或请参阅Disable gesture recognizer iOS