【问题标题】:UIPanGestureRecognizer: recognize if object is held fixed in an area and gesture recognizer state is not endedUIPanGestureRecognizer:识别对象是否固定在一个区域中并且手势识别器状态未结束
【发布时间】:2013-02-19 10:58:44
【问题描述】:

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

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

【问题讨论】:

    标签: ios uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多