如何检测同时手势的结束? (iOS)

Posted

技术标签:

【中文标题】如何检测同时手势的结束? (iOS)【英文标题】:How to detect the end of simultaneous gestures? (iOS) 【发布时间】:2011-10-18 12:00:23 【问题描述】:

在我的应用中,我同时使用 UIPinchGestureRecognizer、UIRotationGestureRecognizer 和 UIPanGestureRecognizer 来缩放、旋转和移动图像。

方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 总是返回 YES 并且图像处理效果很好,但是......如何检测所有同时手势的结束,以便我可以重置图像?

【问题讨论】:

【参考方案1】:

一个简单的解决方案怎么样,比如计算当前正在处理的手势,并在所有手势结束时采取行动?

.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];
        
    

【讨论】:

当您涉及点击识别器时,此方法将失败。他们似乎在UIGestureRecognizerStateBegan 中发送消息,导致handledGesturesCount 减少到0 以下。

以上是关于如何检测同时手势的结束? (iOS)的主要内容,如果未能解决你的问题,请参考以下文章

如何检测 UISwipeGestureRecognizer 的结束?

如何确定平移手势的真实结束速度?

透明后如何检测 UIView 上的手势?

IOS:如何仅使用轻击手势同时允许触发的转场和发送的动作

如何实现检测点击事件的全局手势识别器?

使用手势的 iOS Phonegap 滑动检测