UILongPressGestureRecognizer停止手柄而不停止触摸 [英] UILongPressGestureRecognizer stop handle without stop touching

查看:106
本文介绍了UILongPressGestureRecognizer停止手柄而不停止触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using UILongPressGestureRecognizer class to handle if one item is being selected.

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

The logic is as follows: User press during 1 second an item (UIView subclass). Once the gesture is detected, the item is highlighted and moveable.

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

The user must move this item across the screen without stop touching it.

我面临的问题是手势识别阴影触摸开始/移动/结束物品类来安排移动。

The problem I'm facing is the gesture recognized shadows touchesBegan/Move/Ended necessary for the item class to arrange the movement.

我尝试过检测到一次识别的手势并选择该项目。但仍然发送消息到手势的句柄而不是调用触摸方法。

I tried to remove the gesture recognized once is detected and the item selected. But still sending messages to the handle of gesture instead of call touches methods.

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

Anyone knows any way to stop "listening" the gesture recognizer without leave the finger of the screen?

谢谢。

此处代码:

-(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分支中看到的那样,委托调用允许所有程序将此项目标记为已选中,并在删除识别器之后。

As you can see in the else branch the delegate calls enables all procedures to mark this item as selected, and just after remove the recognizers.

我缺少什么?

- 编辑 -

完成!这有效:

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

Does the custom UIView class have its own touch handling code? If not, a simple solution is to set the allowableMovement property of the UILongPressGestureRecognizer to CGFLOAT_MAX, or some big number, and use the gesture update callbacks to drag your custom view around. You can get the displacement using the - (CGPoint)locationInView:(UIView *)view method on the superview, and compare its position to when the recognizer began.

这篇关于UILongPressGestureRecognizer停止手柄而不停止触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆