stalk
stalk

Reputation: 97

UIScrollView, PanGestureRecognizer and 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?

Upvotes: 1

Views: 1673

Answers (2)

jaime
jaime

Reputation: 926

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

Upvotes: 0

stalk
stalk

Reputation: 97

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

Upvotes: 2

Related Questions