基于UIGesture识别器状态执行操作

时间:2011-05-24 09:23:08

标签: iphone objective-c uigesturerecognizer

这个想法是让进程附加到UIGestureRecognizer状态更具体地说我想要的是将手势参数记录到可变数组中,当识别出手势状态时以及当手势结束时输出可变数组。

最初我以为我可以使用两个while循环:

-(void)pincherAction:(UIPinchGestureRecognizer *)sender 
{  
   if ([sender state] == UIGestureRecognizerStateBegan) 
   {
      pinchGesture = YES;
      while (pinchGesture) 
      {
         if ([sender state] == UIGestureRecognizerStateEnded) 
         {
             pinchGesture = NO;
         } else {
            //do array stuff here
         }
      }
   }

   if ([sender state] == UIGestureRecognizerStateEnded) 
   {
        pinchGesture = NO;
        while (!pinchGesture) 
        {
            if ([sender state] == UIGestureRecognizerStateEnded) 
            {
                pinchGesture = YES;
            } else {
                 //output array here
            }
        }
    }
}

问题是,一旦进入while循环,它就不会发现任何状态变化。我需要在单独的线程上运行此循环吗?或者我做了一些根本错误的事情!?

1 个答案:

答案 0 :(得分:1)

我刚刚创建了一个带有UIPinchGestureRecognizer的简单项目。在awakeFromNib中为viewController设置它:

- (void)awakeFromNib
{
    UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                                              action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:rec];
    [rec release];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    NSLog(@"%f %f %d", sender.velocity, sender.scale, sender.state);
}

现在,当我只是做一些捏合并将两个点左右移动一小段时,pinchAction方法经常被调用:

2011-05-24 12:40:28.609 Pinch[1216:207] -3.506220 1.430036 2
2011-05-24 12:40:28.625 Pinch[1216:207] -2.277907 1.381398 2
2011-05-24 12:40:28.642 Pinch[1216:207] -2.738462 1.347324 2
2011-05-24 12:40:28.659 Pinch[1216:207] -2.095164 1.308440 2
2011-05-24 12:40:28.676 Pinch[1216:207] -2.383961 1.264628 2
2011-05-24 12:40:28.692 Pinch[1216:207] -2.357965 1.240309 2
2011-05-24 12:40:28.726 Pinch[1216:207] -1.192955 1.225726 2
2011-05-24 12:40:28.760 Pinch[1216:207] -0.397422 1.215989 2
2011-05-24 12:40:28.776 Pinch[1216:207] -0.291231 1.211164 2
2011-05-24 12:40:28.793 Pinch[1216:207] -0.366087 1.201407 2
2011-05-24 12:40:29.094 Pinch[1216:207] -0.366087 1.201407 3

立即认识到状态的变化。但是,它只记录您在手指周围移动时的更改。

您不需要while循环。