ios - UIPanGestureRecognizer:识别对象是否固定在某个区域并且手势识别器状态未结束

标签 ios uigesturerecognizer uipangesturerecognizer

我正在使用 UIPanGestureRecognizer 来识别对象的拖动运动,它在以下状态下工作正常:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[self.view addGestureRecognizer:panGesture];
[panGesture release];

- (void)panAction:(UIPanGestureRecognizer *)gR 
{
    switch (gR.state) 
    {       
        case UIGestureRecognizerStateBegan:
            NSLog(@"drag began");
            break;

        case UIGestureRecognizerStateChanged:
            NSLog(@"drag moved");
            break;

        case UIGestureRecognizerStateEnded:
            NSLog(@"drag ended");
            break;

        default:
            break;
    }
}

现在我的问题是,是否可以检测用户开始拖动对象并在某个点停止拖动并将对象保持在固定点的情况?我已经检查过,当用户停止移动对象时,它会停止记录“已更改”,直到用户开始移动对象或释放它(在这种情况下,它会记录“已结束”)。 这是一个示例日志:

2013-02-19 16:36:**01.181** Project[24201:10a03] drag began
2013-02-19 16:36:**14.004** Project[24201:10a03] drag moved
2013-02-19 16:36:14.221 Project[24201:10a03] drag moved
2013-02-19 16:36:**14.894** Project[24201:10a03] drag ended

您可以看到在开始、更改和结束之间有一些停顿(此时对象固定在一个点)。是否有可能检测到这种情况?如果是这样,如何? 任何帮助将不胜感激,在此先感谢。

更新

我试过 UILongPressGestureRecognizer:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[longPressGesture setDelegate:self];
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture release];

-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gR
{
    switch (gR.state)
    {
        case UIGestureRecognizerStateBegan:
            NSLog(@"long press began");
            break;

        case UIGestureRecognizerStateCancelled:
            NSLog(@"long press cancelled");
            break;

        case UIGestureRecognizerStateEnded:
            NSLog(@"long press ended");
            break;

        default:
            break;
    }
}

还有委托(delegate)方法:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

现在,两个识别器都被单独识别了。如果先执行长按手势,则同时识别两个手势:

2013-02-19 17:19:13.382 Project[24357:10a03] long press began
2013-02-19 17:19:14.901 Project[24357:10a03] drag began
2013-02-19 17:19:14.937 Project[24357:10a03] drag moving
2013-02-19 17:19:15.025 Project[24357:10a03] drag moving
2013-02-19 17:19:16.317 Project[24357:10a03] long press ended
2013-02-19 17:19:16.317 Project[24357:10a03] drag ended

但是如果拖动手势先 Action ,它就不再识别长按,除非拖动结束:

2013-02-19 17:21:05.985 Project[24357:10a03] drag began
2013-02-19 17:21:06.001 Project[24357:10a03] drag moving
2013-02-19 17:21:06.018 Project[24357:10a03] drag moving
2013-02-19 17:21:**06.052** Project[24357:10a03] drag moving
2013-02-19 17:21:**17.786** Project[24357:10a03] drag moving
2013-02-19 17:21:17.818 Project[24357:10a03] drag moving
2013-02-19 17:21:17.851 Project[24357:10a03] drag moving
2013-02-19 17:21:19.324 Project[24357:10a03] drag ended
2013-02-19 17:21:20.388 Project[24357:10a03] long press began
2013-02-19 17:21:21.188 Project[24357:10a03] long press ended

我需要第二种情况来检测拖动开始后的长按。

最佳答案

您可以在 UIGestureRecognizerStateChanged 上安排一个 NSTimer,如果它存在的话,在它失效之后,然后在计时器的选择器中做任何您想做的事情。

if(self.myTimer)
     [self.myTimer invalidate];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:PAUSE_INTERVAL target:self selector:@selector(userPaused:) userInfo:nil repeats:NO];

关于ios - UIPanGestureRecognizer:识别对象是否固定在某个区域并且手势识别器状态未结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14955771/

相关文章:

ios - Phonegap/iOS - 如何推送 childviewcontroller?

ios - GestureRecognizer 不能动态工作/在额外的类 swift 中工作

iphone - 手势识别器,奇怪的行为(乘数效应)iOS

iphone - 如何在执行拖动手势时平滑地为 CGAffineTransform 设置动画

ios - swift : Create region to UIPanGesture

ios - 如何限制平移和缩放区域?

iphone - 所有 View Controller 中的摇动事件检测

ios - 如何修复自动布局中的标签和按钮标题?

ios - 可以让屏幕上的元素立即旋转

iOS UIPanGestureRecognizer 防止滚动