实现多个手势同时执行

ios开发中,默认是在同一时间只能有一个手势被执行,要实现多个手势同时进行,须实现 UIGestureRecognizerDelegate,并重写函数

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer


以Pinch和Rotation手势为例,首先是添加手势:

[cpp]  view plain copy
  1.        UIGestureRecognizer *recognizer;  
  2. recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationFrom:)];  
  3. recognizer.cancelsTouchesInView = NO;  
  4. recognizer.delegate = self;  
  5. [self.view addGestureRecognizer:recognizer];  
  6. [recognizer release];  
  7.   
  8. UIGestureRecognizer *recognizer2;  
  9. recognizer2 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];  
  10. recognizer2.cancelsTouchesInView = NO;  
  11. recognizer2.delegate = self;  
  12. [self.view addGestureRecognizer:recognizer2];  
  13. [recognizer2 release];  


接着重写上面提到的函数,只需返回YES即可。

[cpp]  view plain copy
  1. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer   
[cpp]  view plain copy
  1.   shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  
  2.   
  3. return YES;  

你可能感兴趣的:(实现多个手势同时执行)