开发者

Performing actions based on UIGesture Recognizer States

开发者 https://www.devze.com 2023-03-08 10:14 出处:网络
The idea is to have processes attaches to UIGestureRecognizer states more specifically what I want is to record the gesture parameters into a mutable array, when the gesture state is recognized and wh

The idea is to have processes attaches to UIGestureRecognizer states more specifically what I want is to record the gesture parameters into a mutable array, when the gesture state is recognized and when the gesture is ended output the mutable array.

Initially I thought I could use two while loops :

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

The problem is that once in the while loop it doesnt pick up any state changes. Would I need to run this loop on a separate thread? or am I doing something fundamentally wrong!?


I've just created a simple project with a UIPinchGestureRecognizer. Setting it up in the awakeFromNib for a 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);
}

Now, when I just do some pinching and move the two points around for a small around the pinchAction method is called very often:

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

And a change in state is recognized immediately. However it only records changes when you move around the fingers.

You don't need the while loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消