iphone - UILongPressGestureRecognizer 停止 handle 而不停止触摸

标签 iphone ios uigesturerecognizer

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

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

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

我面临的问题是手势识别的阴影 touchesBegan/Move/Ended 是项目类安排移动所必需的。

我试图删除一旦检测到识别的手势并选择项目。但仍然向手势句柄发送消息,而不是调用触摸方法。

谁知道有什么方法可以在手指不离开屏幕的情况下停止“收听”手势识别器?

谢谢。

这里是代码:

-(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 分支中看到的,委托(delegate)调用使所有过程能够将此项标记为已选中,并且在删除识别器之后。

我错过了什么?

--编辑--

完成!这有效:

#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 或某个较大的数字,并使用手势更新回调拖动您的自定义 View 。您可以使用父 View 上的 - (CGPoint)locationInView:(UIView *)view 方法获取位移,并将其位置与识别器开始时的位置进行比较。

关于iphone - UILongPressGestureRecognizer 停止 handle 而不停止触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688139/

相关文章:

iphone - YouTube 上传质量

iphone - MBProgressHUD 的替代品?

ios - SwiftyStoreKit 如何检查用户是否购买了产品?

ios - 如何使用 UIGestureRecognizer 隐藏 PKHUD?

ios - 如何从 TableView 单元格中的图像手势获取indexPath

ios - 如何在UITableView的UiLabel中单击特定位置?

iphone - XCode 中的简单 Torch 应用

ios - View 未填充数据 AFNetworking

ios - Xcode 根据按钮在新 View 中加载数据

iphone - iOS 上是否支持 SMB/samba?