UILongPressGestureRecognizer停止手柄而不停止触摸

Nem*_*Sys 6 iphone uigesturerecognizer ios

我正在使用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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在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];



    }
}
Run Code Online (Sandbox Code Playgroud)

问候!

Sha*_* TK 1

我心中有两种解决方案。

\n\n
    \n
  1. 对于动画 uiview,请编写一个从 UIView 类继承的新类并实现触摸委托,而不是编写 Gustures 来处理动画(如果触摸委托未在当前类中触发)。
  2. \n
\n\n

2.触发一次后,我已成功删除UILongPressGestureRecognizer。

\n\n

请参考下面的代码。如果您有任何疑问,请询问我

\n\n

我已完成的步骤

\n\n

当主视图加载时,我已将 UIView 作为“myView”添加到我的主视图中。

\n\n

我已将标签赋予 myView(您可以给出 1,2,3\xe2\x80\xa6etc),以区分点击视图与主视图子视图。

\n\n

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

\n\n

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

\n\n

然后我用条件 Tag == 1 迭代 mainView Subviews

\n\n

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

\n\n

所以 NSLog(@"手势已删除"); 和 NSLog(@"moveMe"); 只会一次登录控制台。

\n\n

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

\n\n

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

\n\n

代码

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

或者请参阅禁用手势识别器 iOS

\n