Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling with two fingers with a UIScrollView

I have an app where my main view accepts both touchesBegan and touchesMoved, and therefore takes in single finger touches, and drags. I want to implement a UIScrollView, and I have it working, but it overrides the drags, and therefore my contentView never receives them. I'd like to implement a UIScrollview, where a two finger drag indicates a scroll, and a one finger drag event gets passed to my content view, so it performs normally. Do I need create my own subclass of UIScrollView?

Here's my code from my appDelegate where I implement the UIScrollView.

@implementation MusicGridAppDelegate  @synthesize window; @synthesize viewController; @synthesize scrollView;   - (void)applicationDidFinishLaunching:(UIApplication *)application {          // Override point for customization after app launch         //[application setStatusBarHidden:YES animated:NO];     //[window addSubview:viewController.view];      scrollView.contentSize = CGSizeMake(720, 480);     scrollView.showsHorizontalScrollIndicator = YES;     scrollView.showsVerticalScrollIndicator = YES;     scrollView.delegate = self;     [scrollView addSubview:viewController.view];     [window makeKeyAndVisible]; }   - (void)dealloc {     [viewController release];     [scrollView release];     [window release];     [super dealloc]; } 
like image 423
Craig Avatar asked Apr 24 '09 19:04

Craig


2 Answers

In SDK 3.2 the touch handling for UIScrollView is handled using Gesture Recognizers.

If you want to do two-finger panning instead of the default one-finger panning, you can use the following code:

for (UIGestureRecognizer *gestureRecognizer in scrollView.gestureRecognizers) {          if ([gestureRecognizer  isKindOfClass:[UIPanGestureRecognizer class]]) {         UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer;         panGR.minimumNumberOfTouches = 2;                    } } 
like image 97
Kenshi Avatar answered Sep 23 '22 21:09

Kenshi


For iOS 5+, setting this property has the same effect as the answer by Mike Laurence:

self.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2; 

One finger dragging is ignored by panGestureRecognizer and so the one finger drag event gets passed to the content view.

like image 25
Guto Araujo Avatar answered Sep 25 '22 21:09

Guto Araujo