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

发布于 2024-12-10 16:21:51 字数 243 浏览 0 评论 0原文

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

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

In my app I use UIPinchGestureRecognizer, UIRotationGestureRecognizer and UIPanGestureRecognizer simultaneously for scaling, rotating and moving an image.

The method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: always return YES and the image manipulation works great, but... how can I detect the end of ALL simultaneous gestures, so I can reset the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-12-17 16:21:51

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

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

How about a simple solution like counting gestures that are currently being handled, and acting when all of them end?

.h file:

int handledGesturesCount;

.m file:

- (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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文