UIPanGestureRecognizer:识别对象是否在区域中保持固定并且手势识别器状态未结束

时间:2013-02-19 10:58:44

标签: 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;
    }
}

还有委托方法:

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

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

1 个答案:

答案 0 :(得分:2)

您可以在NSTimer无效后UIGestureRecognizerStateChanged安排if(self.myTimer) [self.myTimer invalidate]; self.myTimer = [NSTimer scheduledTimerWithTimeInterval:PAUSE_INTERVAL target:self selector:@selector(userPaused:) userInfo:nil repeats:NO]; ,如果它存在,则在计时器的选择器中执行任何操作。

{{1}}