Recognition of swipe with two touches in UIScrollView
I have an UIScrollView with full screen size (iPad, 1024x768, landscape mode). So I need to recognize swipe with two finger in any direction over it. That's what i have (mainScroll is the property of my class):
//MyViewController.h
- (void)loadView {
mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
mainScroll.contentSize = CGSizeMake(1024*pageNumber, 768);
mainScroll.pagingEnabled = YES;
mainScroll.delegate = self;
[self.view addSubview:mainScroll];
GestureRecognizer *tapInterceptor = [[GestureRecognizer alloc] init];
tapInterceptor.numberOfTouchesRequired = 2;
tapInterceptor.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRec开发者_运维问答ognizerDirectionRight;
[mainScroll addGestureRecognizer:tapInterceptor];
mainScroll.userInteractionEnabled = YES;
}
and
//GestureRecognizer.h
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] > 1)
NSLog(@"Started");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] > 1)
NSLog(@"Moved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] > 1)
NSLog(@"Ended");
}
I had to add condition
if ([touches count] > 1)
because it works not only for two (ore more) touches, but also for one.
But swipe (with two touches) is still scrolls my UIScrollView. How can i prevent it? So how can I recognize swipe without any effect on my scrollView?
The documentation of the UIGestureRecognizer gives a good explanation of each of the three following properties:
cancelsTouchesInView — If a gesture recognizer recognizes its gesture, it unbinds the remaining touches of that gesture from their view (so the window won’t deliver them). The window cancels the previously delivered touches with a (touchesCancelled:withEvent:) message. If a gesture recognizer doesn’t recognize its gesture, the view receives all touches in the multi-touch sequence.
delaysTouchesBegan — As long as a gesture recognizer, when analyzing touch events, has not failed recognition of its gesture, the window withholds delivery of touch objects in the UITouchPhaseBegan phase to the attached view. If the gesture recognizer subsequently recognizes its gesture, the view does not receive these touch objects. If the gesture recognizer does not recognize its gesture, the window delivers these objects in an invocation of the view’s touchesBegan:withEvent: method (and possibly a follow-up touchesMoved:withEvent: invocation to inform it of the touches current location).
delaysTouchesEnded — As long as a gesture recognizer, when analyzing touch events, has not failed recognition of its gesture, the window withholds delivery of touch objects in the UITouchPhaseEnded phase to the attached view. If the gesture recognizer subsequently recognizes its gesture, the touches are cancelled (in a touchesCancelled:withEvent: message). If the gesture recognizer does not recognize its gesture, the window delivers these objects in an invocation of the view’s touchesEnded:withEvent: method.
There is much more here: UIGestureRecognizer.
I'm thinking you want either the "delaysTouchesBegan" or "delaysTouchesEnded" so that the scroll view won't receive any of the touches (IE scrolling the view) until the gesture fails it's requirements.
精彩评论