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];
}
Bad news: iPhone SDK 3.0 and up, don't pass touches to
-touchesBegan:
and -touchesEnded:
**UIScrollview
**subclass methods anymore. You can use thetouchesShouldBegin
andtouchesShouldCancelInContentView
methods that is not the same.If you really want to get this touches, have one hack that allow this.
In your subclass of
UIScrollView
override thehitTest
method like this:This will pass to you subclass this touches, however you can't cancel the touches to
UIScrollView
super class.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 iOS 5+, setting this property has the same effect as the answer by Mike Laurence:
One finger dragging is ignored by panGestureRecognizer and so the one finger drag event gets passed to the content view.
What I do is have my view controller set up the scroll view:
And in my child view I have a timer because two-finger touches usually start out as one finger followed quickly by two fingers.:
If a second touchesBegan: event comes in with more than one finger, the scroll view is allowed to cancel touches. So if the user pans using two fingers, this view would get a
touchesCanceled:
message.Check out my solution:
It does not delays the first touch and does not stop when the user touches with two fingers after using one. Still it allows to cancel a just started one touch event using a timer.
Yes, you'll need to subclass
UIScrollView
and override its -touchesBegan:
and-touchesEnded:
methods to pass touches "up". This will probably also involve the subclass having aUIView
member variable so that it knows what it's meant to pass the touches up to.