UIScrollView、PanGestureRecognizer 和 iOs 4

发布于 2024-12-26 04:02:50 字数 576 浏览 0 评论 0原文

我有一个可滚动视图,我需要手势识别器,该手势识别器将在不滚动主视图的情况下触发。

我使用过这段代码:

UISwipeGestureRecognizer *upRecognizer = 
    [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(upSwipeHandle:)];
upRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[upRecognizer setNumberOfTouchesRequired:2];
[controller.view addGestureRecognizer:upRecognizer];
[[controller.scrollView panGestureRecognizer]
    requireGestureRecognizerToFail:upRecognizer];

但它似乎只在 iOS 5 中工作。我如何在 iOS 4 中使用相同的识别器?

I have a scrollable view and I need the gesture recognizer that will fire without scrolling of main view.

I've used this code:

UISwipeGestureRecognizer *upRecognizer = 
    [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(upSwipeHandle:)];
upRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[upRecognizer setNumberOfTouchesRequired:2];
[controller.view addGestureRecognizer:upRecognizer];
[[controller.scrollView panGestureRecognizer]
    requireGestureRecognizerToFail:upRecognizer];

But it seems to work only in iOS 5. How can I use the same recognizer in iOS 4?

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

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

发布评论

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

评论(2

攀登最高峰 2025-01-02 04:02:50

我通过使用 UIGestureRecognizer 委托方法解决了我的问题。

只需添加

upRecognizer.delegate = self;

这个方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        return YES;
    }

    return NO;
}  

I Solve my problem by using UIGestureRecognizer delegate method.

just add

upRecognizer.delegate = self;

and this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        return YES;
    }

    return NO;
}  
家住魔仙堡 2025-01-02 04:02:50

我遇到了同样的问题。我在 4.3 模拟器中运行了类似的代码,但该代码失败了,因为 scrollView.panGestureRecognizer 仅在 iOS 5.0+ 中可用。

我通过向 UIScrollView 添加一个类别来解决这个问题。

@interface UIScrollView(panGestureRecognizer)
@end
@implementation UIScrollView(panGestureRecognizer)
// iOS5 has a panGestureRecognizer property
// Add a getter for iOS 4.3+
- (UIPanGestureRecognizer *)panGestureRecognizer
{
    for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers)
    {
        if ([gestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]]) {
            return (UIPanGestureRecognizer *)gestureRecognizer;
        }
    }
    return nil;
}
@end

I ran into the same issue. I ran similar code in the 4.3 simulator and that code failed because scrollView.panGestureRecognizer is only available in iOS 5.0+.

I solved it by adding a category to UIScrollView.

@interface UIScrollView(panGestureRecognizer)
@end
@implementation UIScrollView(panGestureRecognizer)
// iOS5 has a panGestureRecognizer property
// Add a getter for iOS 4.3+
- (UIPanGestureRecognizer *)panGestureRecognizer
{
    for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers)
    {
        if ([gestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]]) {
            return (UIPanGestureRecognizer *)gestureRecognizer;
        }
    }
    return nil;
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文