2011-10-18 57 views
3

在我的應用程序中,我同時使用UIPinchGestureRecognizer,UIRotationGestureRecognizer和UIPanGestureRecognizer來縮放,旋轉和移動圖像。如何檢測同時手勢的結束? (iOS)

方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:總是返回YES,並且圖像處理效果很好,但是...如何檢測所有同時手勢的結束,以便我可以重置圖像?

回答

7

如何處理當前正在處理的手勢等簡單解決方案,並在所有手勢都結束時執行操作?

.h文件中:

int handledGesturesCount; 

.m文件:

- (id)init { 
    (...) 
    handledGesturesCount = 0; 
} 

// gesture handlers - the code for -pinch: repeats for -pan: and -rotate: 
- (void)pinch:(UIPinchGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     handledGesturesCount += 1; 
    } else if (recognizer.state == UIGestureRecognizerStateEnded || 
       recognizer.state == UIGestureRecognizerStateCancelled || 
       recognizer.state == UIGestureRecognizerStateFailed) 
    { 
     handledGesturesCount -= 1; 
     if (handledGesturesCount == 0) { 
      [self resetImage]; 
     } 
    } 
} 

- (void)pan:(UIPanGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     handledGesturesCount += 1; 
    } else if (recognizer.state == UIGestureRecognizerStateEnded || 
       recognizer.state == UIGestureRecognizerStateCancelled || 
       recognizer.state == UIGestureRecognizerStateFailed) 
    { 
     handledGesturesCount -= 1; 
     if (handledGesturesCount == 0) { 
      [self resetImage]; 
     } 
    } 
} 

- (void)rotate:(UIRotationGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     handledGesturesCount += 1; 
    } else if (recognizer.state == UIGestureRecognizerStateEnded || 
       recognizer.state == UIGestureRecognizerStateCancelled || 
       recognizer.state == UIGestureRecognizerStateFailed) 
    { 
     handledGesturesCount -= 1; 
     if (handledGesturesCount == 0) { 
      [self resetImage]; 
     } 
    } 
} 
+1

非常感謝你,它的工作原理。 –

+0

當您使用抽頭識別器時,此方法將失敗。他們似乎在'UIGestureRecognizerStateBegan'時發送消息,導致'handledGesturesCount'減少到0以下。 –