当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
在我的应用程序中,我同时使用UIPinchGestureRecognizer,UIRotationGestureRecognizer和UIPanGestureRecognizer进行缩放,旋转和移动图像. 方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:始终返回
在我的应用程序中,我同时使用UIPinchGestureRecognizer,UIRotationGestureRecognizer和UIPanGestureRecognizer进行缩放,旋转和移动图像.

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

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

.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];
        }
    }
}
网友评论