UILongPressGestureRecognizer停止手柄而不停止触摸

我正在使用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]; } } 

问候!

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

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

  1. 为了动画uiview,请写一个新类,它inheritance自UIView类并实现触摸委托,而不是编写Gustures来处理动画(如果触摸委托没有在当前类中触发)。

2.我在触发一次后成功删除了UILongPressGestureRecognizer。

如果您有任何疑问,请参考以下代码

我完成的步骤

当主视图加载时,我在主视图中添加了一个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"); } 

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