5

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

还有委托方法:

-(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

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

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

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

4

1 回答 1

2

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

if(self.myTimer)
     [self.myTimer invalidate];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:PAUSE_INTERVAL target:self selector:@selector(userPaused:) userInfo:nil repeats:NO];
于 2013-02-19T11:10:54.387 回答