iOS7 & iOS8
I need to disable 2 or three fingers scrolling in UIScrollview
.
I tried :
[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];
But it has no effect. It is still possible to scroll with 2 fingers.
If i tried to set max and min to 2. One finger scrolling was disabled but 3 fingers scrolling possible :(
I tried this too, but without success:
for (UIGestureRecognizer* pan in self.scrollView.gestureRecognizers) {
OTTNSLog(@"touches: %ld", (unsigned long)pan.numberOfTouches);
if ([pan isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) pan;
mpanGR.minimumNumberOfTouches = 1;
mpanGR.maximumNumberOfTouches = 1;
}
if ([pan isKindOfClass:[UISwipeGestureRecognizer class]])
{
UISwipeGestureRecognizer *mswipeGR = (UISwipeGestureRecognizer *) pan;
mswipeGR.numberOfTouchesRequired = 1;
}
}
Does anybody know, how it solve this ?
Thanks.
PROBLEM:
When the
UIPanGestureRecognizer
is underlying aUIScrollView
- which unfortunately does also effectUIPageViewController
- themaximumNumberOfTouches
is not behaving as expected, theminimumNumberOfTouches
however always limits the lower end correctly.When monitoring these parameters they report back correct values - they seem to do their job - it's just that
UIScrollView
itself doesn't honor them and ignores their settings!SOLUTION:
Set the
minimumNumberOfTouches
to the desired value e.g. 1 and - very importantly - themaximumNumberOfTouches
to 2 !!!Conform to the
UIGestureRecognizerDelegate
protocol in your scrollView's @interface declaration. You don't have to set thepanGestureRecognizer.delegate
for aUIScrollView
!!! The delegate is already set becauseUIScrollView
requires to be the delegate of its ownpan/pinchGestureRecognizer
.Then implement the
UIGestureRecognizer
delegate method:AN EVEN SAFER VERSION:
If you have a custom scrollView class and wanna be on the VERY safe side you can also add one more line of code to disambiguate against other scrollViews:
ADDITIONAL INFORMATION:
The NSLogs tell you the number of touches. If you set both the min and max to the same value (like 1 in the example above) the
if-loop
would never be triggered... ;-)That is why
maximumNumberOfTouches
has to be at leastminimumNumberOfTouches + 1
GEEK SECTION:
This is the way to access the underlying scrollView that is responsible for the paging of a
UIPageViewController
. Put this code in the e.g.viewDidLoad:
of theUIPageViewController
(self).If you don't have access to the scrollView at all - like for a
UIPageViewController
in a dynamicUITableViewCell
where creation and cell reuse happens at runtime and no outlets can be set on its contentViews - put a category onUIScrollView
and override the delegate method there. But be careful! This effects every scrollView in your application - so do proper introspection (class-checking) like in my 'EVEN SAFER' example above... ;-)SIDENOTE:
Unfortunately the same trick doesn't work with the
pinchGestureRecognizer
onUIScrollView
because it doesn't expose amin/maxNumberOfTouches
property. When monitored it always reports 2 touches (which you obviously need to pinch) - so its internalmin/maxNumberOfTouches
seem to have been set both to 2 - even ifUIScrollView
isn't honoring its own settings and keeps happily pinching with any numbers of fingers (more than 2). So there is no way to restrict pinching to a limited amount of fingers...There is no specific method available for this just do some tricks but results are only 75%.
Add swipe gestures(4 directions each) and double tap gesture to your UIScrollview..Then use below code..
I can confirm this is still an issue in iOS 8, but only when the UIPanGestureRecognizer is underlying a UIScrollView. Creating a UIView with a fresh UIPanGestureRecognizer and setting its maximumNumberOfTouches property works as expected.
Interesting note: if you query the UIScrollView's UIPanGestureRecognizer while you're scrolling, it reports the number of touches as less than or equal to the maximum. In other words, if you set the maximum to 2, and scroll with 3 fingers, it reports the gesture as a 2-finger scroll. Subsequently letting up fingers one at a time while continuing to scroll usually (but not consistently) reduces the reported number of touches as well — so if you go from 3 -> 2 -> 1 fingers, it will register 2 -> 1 -> 0 touches, and stop scrolling while you still have 1 finger on the device.
Submitted rdar://20890684 and copied to http://openradar.appspot.com/radar?id=6191825677189120. Please feel free to dupe.
This will do the job for you: