UIScrollView ignores maximumNumberOfTouches parameter

Originator:list
Number:rdar://20890684 Date Originated:10 May 2015
Status:Open Resolved:
Product:iOS SDK Product Version:8.3 (12F70)
Classification:Serious Bug Reproducible:Always
 
Summary:
Setting the maximumNumberOfTouches parameter on the UIPanGestureRecognizer underlying a UIScrollView yields different results from setting the same parameter on an independently initialized UIPanGestureRecognizer.

Steps to Reproduce:
(Sample code below)
1. Initialize a UIScrollView.
2. Set the UIScrollView's PanGestureRecognizer's maximumNumberOfTouches to 2.
3. Scroll with 3 fingers in the UIScrollView.

Expected Results:
UIScrollView should ignore 3-fingered scrolling. Underlying UIPanGestureRecognizer should not report any gesture.

Actual Results:
UIScrollView scrolls. Underlying UIPanGestureRecognizer reports a gesture with numberOfTouches==2.

Notes:
When I set maximumNumberOfTouches to 2 in an independently initialized UIPanGestureRecognizer attached to a fresh UIView, the behavior is as expected: it reports a pan gesture when panning with 1 or 2 fingers, but ignores pans with 3 fingers.

Additionally, in the UIScrollView setup, if you start scrolling with 3 fingers, then reduce to 2 fingers, and then to 1, the number of reported touches sometimes goes from 2 to 1 to 0, and at 0, it stops scrolling, even though there is still one finger scrolling on the device.

Sample UIViewController implementation below, which creates a UIView and UIScrollView, demonstrating the different behavior of two identically configured UIPanGestureRecognizers.


#import "ViewController.h"

@interface ViewController () {
	UILabel *_panLabel, *_scrollLabel;
}

@end

@implementation ViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	
	CGRect topRect, bottomRect;
	CGRectDivide(self.view.frame, &topRect, &bottomRect, CGRectGetMidY(self.view.frame), CGRectMinYEdge);
	
	// This section works as expected.
	UIPanGestureRecognizer *panRecog = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
	panRecog.maximumNumberOfTouches=2;
	panRecog.minimumNumberOfTouches=1;
	UIView *panView = [[UIView alloc] initWithFrame:topRect];
	[panView addGestureRecognizer:panRecog];
	[self.view addSubview:panView];
	_panLabel = [[UILabel alloc] initWithFrame:topRect];
	_panLabel.text = @"Pan area\n";
	_panLabel.numberOfLines = 2;
	_panLabel.textAlignment = NSTextAlignmentCenter;
	[panView addSubview:_panLabel];
	
	// This section does not work as expected. Scrolling with 3 fingers reports 2 fingers detected.
	// Even weirder, if you start scrolling with 3 fingers, then reduce to 2, then to 1, it reports 2 -> 1 -> 0, and stops scrolling when you're down to 1 finger.
	UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:bottomRect];
	CGSize tripleSize = CGSizeMake(bottomRect.size.width, bottomRect.size.height * 3);
	scrollView.backgroundColor = [UIColor yellowColor];
	[scrollView setContentSize:tripleSize];
	[self.view addSubview:scrollView];
	[scrollView.panGestureRecognizer addTarget:self action:@selector(handlePan:)];
	scrollView.panGestureRecognizer.maximumNumberOfTouches=2;
	scrollView.panGestureRecognizer.minimumNumberOfTouches=1;
	_scrollLabel = [[UILabel alloc] initWithFrame:bottomRect];
	_scrollLabel.text = @"Scroll area\n";
	_scrollLabel.numberOfLines = 2;
	_scrollLabel.textAlignment = NSTextAlignmentCenter;
	[self.view addSubview:_scrollLabel];
}

- (void)handlePan:(UIPanGestureRecognizer *)sender {
	long touches = sender.numberOfTouches;
	NSString *touchReport = @"";	// Clear 2nd line of label when number of touches is zero
	if (touches)
		touchReport = [NSString stringWithFormat:@"Detected pan with %ld touches",touches];
	if ([sender.view isKindOfClass:[UIScrollView class]]) {
		_scrollLabel.text = [NSString stringWithFormat:@"Scroll area\n%@",touchReport];
	} else {
		_panLabel.text = [NSString stringWithFormat:@"Pan area \n%@",touchReport];
	}
}

@end

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!