UIScrollView,PanGestureRecognizer和iOs 4

时间:2012-01-10 18:16:15

标签: ios ios4 uigesturerecognizer uipangesturerecognizer

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

我用过这段代码:

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中使用相同的识别器?

2 个答案:

答案 0 :(得分:2)

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

只需添加

upRecognizer.delegate = self;

和这个方法:

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

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

    return NO;
}  

答案 1 :(得分:0)

我遇到了同样的问题。我在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