使用 setEnabled:NO 禁用 UIPinchGestureRecognizer

发布于 2024-12-28 06:37:26 字数 857 浏览 0 评论 0原文

我的滚动视图上附加了一个捏合手势识别器(一个位于默认识别器的顶部)。我试图在某些缩放级别启用它并禁用它,但它不起作用。我使用命令:

[self.pinchGesture setEnabled:NO];
and
[self.pinchGesture setEnabled:YES];

启用和禁用捏合手势。我正在尝试调试它,并使用此代码打印出手势的描述:

NSArray *gestures = [self.scrollView gestureRecognizers];
for (UIGestureRecognizer *gesture in gestures) {
    NSLog(@"%s, gesture: %@", __FUNCTION__, [gesture description]);
}

我看到对于我的自定义手势,它看起来像:

gesture: <UIPinchGestureRecognizer: 0x88a62d0; state = Possible; enabled = NO; view = <UIScrollView 0x880c360>; target= <(action=handlePinch

因此,即使它设置为 enabled = NO,捏仍然调用handlePinch:方法。这是有原因的吗?或者我是否需要使用 [self.scrollView setGestureRecognizers:<#(NSArray *)#> 来删除捏合手势?如果我要采用这种方法,我是否必须循环遍历滚动视图的手势,保存对这些手势的引用,而不是设置它们,这样我就不会设置自定义捏合手势?谢谢。

I have a pinch gesture recognizer attached to my scrollView (one on top of the default one). I'm trying to enable it and disable it at certain zoom levels but it is not working. I use the commands:

[self.pinchGesture setEnabled:NO];
and
[self.pinchGesture setEnabled:YES];

to enable and disable the pinch gesture. I am trying to debug it and I use this code to print out the description of my gestures:

NSArray *gestures = [self.scrollView gestureRecognizers];
for (UIGestureRecognizer *gesture in gestures) {
    NSLog(@"%s, gesture: %@", __FUNCTION__, [gesture description]);
}

I see that for my custom gesture it looks like:

gesture: <UIPinchGestureRecognizer: 0x88a62d0; state = Possible; enabled = NO; view = <UIScrollView 0x880c360>; target= <(action=handlePinch

So even though it is set to enabled = NO, the pinch still calls the handlePinch: method. Is there a reason for this? Or do I need to use the [self.scrollView setGestureRecognizers:<#(NSArray *)#> to remove that pinch gesture? If I am to go with that approach, do I have to loop through my gestures for the scrollView, save references to those, than set those so I don't set back my custom pinch gesture? Thanks.

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

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

发布评论

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

评论(1

梦途 2025-01-04 06:37:26

启用的滚动视图只是执行以下操作:启用和禁用滚动视图。手势识别器是一种不同的动物,它有点锁定在你的滚动视图中,但行为几乎独立。为什么不删除手势目标而不是禁用,然后在启用时重新添加目标?或者,设置一个布尔值,您的目标方法将检查该布尔值,并在该布尔值为 YES 时忽略该手势。

The enabled scrollview just does that: enables and disables the scrollview. The Gesture Recognizers are a different animal, that are kind of latched into your scrollview, but behave almost independently. Why not remove the target for the gesture instead of disabling, then add the target back in when enabling? Alternatively, set a boolean that your target method checks and ignores the gesture when the boolean is YES.

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