UIScrollView只用一根手指滚动 [英] UIScrollView scrolling only with one finger
问题描述
iOS7& iOS8
iOS7 & iOS8
我需要在 UIScrollview
中禁用2或3个手指滚动。
I need to disable 2 or three fingers scrolling in UIScrollview
.
我试过:
[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];
但它没有效果。仍然可以用2个手指滚动。
But it has no effect. It is still possible to scroll with 2 fingers.
如果我试图将最大值和最小值设置为2.禁用一个手指滚动,但可以滚动3个手指:(
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 ?
谢谢。
推荐答案
问题:
PROBLEM:
当 UIPanGestureRecognizer
时是 UIScrollView
的基础 - 遗憾的是它也会影响 UIPageViewController
- maximumNumberOfTouches
的行为不符合预期, minimumNumberOfTouches
但是总是正确地限制下端。
When the UIPanGestureRecognizer
is underlying a UIScrollView
- which unfortunately does also effect UIPageViewController
- the maximumNumberOfTouches
is not behaving as expected, the minimumNumberOfTouches
however always limits the lower end correctly.
监控这些参数时,他们会报告回来正确的价值 - 他们似乎做他们的工作 - 只是 UIScrollView
本身不尊重他们并忽略他们的设置!
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:
设置 minimumNumberOfTouches
到所需的值,例如1 - 非常重要的是 - maximumNumberOfTouches
到2 !!!
Set the minimumNumberOfTouches
to the desired value e.g. 1 and - very importantly - the maximumNumberOfTouches
to 2 !!!
myScrollView.panGestureRecognizer.minimumNumberOfTouches = 1;
myScrollView.panGestureRecognizer.maximumNumberOfTouches = 2;
符合 UIGestureRecognizerDelegate $ c $滚动视图的@interface声明中的c>
协议。您不必为 UIScrollView
设置 panGestureRecognizer.delegate
!委托已经设置,因为 UIScrollView
需要成为其自己的 pan / pinchGestureRecognizer
的委托。
Conform to the UIGestureRecognizerDelegate
protocol in your scrollView's @interface declaration. You don't have to set the panGestureRecognizer.delegate
for a UIScrollView
!!! The delegate is already set because UIScrollView
requires to be the delegate of its own pan/pinchGestureRecognizer
.
然后实施 UIGestureRecognizer
委托方法:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%d", gestureRecognizer.numberOfTouches);
NSLog(@"%@", gestureRecognizer.description);
if (gestureRecognizer == self.panGestureRecognizer) {
if (gestureRecognizer.numberOfTouches > 1) {
return NO;
} else {
return YES;
}
} else {
return YES;
}
}
}
一个更安全的版本:
AN EVEN SAFER VERSION:
如果你有一个自定义的scrollView类并想要开启非常安全的一面,你还可以添加一行代码来消除其他scrollViews的歧义:
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:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%d", gestureRecognizer.numberOfTouches);
NSLog(@"%@", gestureRecognizer.description);
if ([gestureRecognizer.view isMemberOfClass:[MY_CustomcrollView class]]) {
if (gestureRecognizer == self.panGestureRecognizer) {
if (gestureRecognizer.numberOfTouches > 1) {
return NO;
} else {
return YES;
}
} else {
return YES;
}
} else {
return YES;
}
}
其他信息:
ADDITIONAL INFORMATION:
NSLog 会告诉您触摸次数。如果您将 min 和 max 设置为相同的值(如上例中的1) if-loop
永远不会被触发...; - )
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... ;-)
这就是为什么 maximumNumberOfTouches
必须至少 minimumNumberOfTouches + 1
That is why maximumNumberOfTouches
has to be at least minimumNumberOfTouches + 1
panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = minimumNumberOfTouches + 1;
GEEK SECTION:
GEEK SECTION:
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
NSLog(@"myPageViewController - SCROLLVIEW GESTURE RECOGNIZERS: %@", view.gestureRecognizers.description);
((UIPanGestureRecognizer *)view.gestureRecognizers[1]).minimumNumberOfTouches = 1;
((UIPanGestureRecognizer *)view.gestureRecognizers[1]).maximumNumberOfTouches = 2;
}
}
这是访问底层scrollView的方法负责分页 UIPageViewController
。将此代码放入例如 viewDidLoad:> c $ c>
UIPageViewController
(self)。
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 the UIPageViewController
(self).
如果您根本无法访问scrollView - 例如 UIPageViewController
在动态 UITableViewCell
中,创建和单元重用在运行时发生,并且不能在其contentView上设置出口 - 在 UIScrollView上放置一个类别
并覆盖那里的委托方法。不过要小心!这会影响应用程序中的每个scrollView - 所以请执行正确的内省(类检查),就像我上面的'EVEN SAFER'示例...; - )
If you don't have access to the scrollView at all - like for a UIPageViewController
in a dynamic UITableViewCell
where creation and cell reuse happens at runtime and no outlets can be set on its contentViews - put a category on UIScrollView
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:
SIDENOTE:
不幸的是,同样的技巧不适用于 pinchGestureRecognizer
UIScrollView
因为它没有公开 min / maxNumberOfTouches
属性。在监控时,它总是报告2次触摸(你显然需要捏) - 所以它的内部 min / maxNumberOfTouches
似乎已被设置为2 - 即使 UIScrollView
也没有遵守自己的设置,并且乐意与之保持愉快任意数量的手指(超过2个)。所以没有办法限制捏到有限数量的手指......
Unfortunately the same trick doesn't work with the pinchGestureRecognizer
on UIScrollView
because it doesn't expose a min/maxNumberOfTouches
property. When monitored it always reports 2 touches (which you obviously need to pinch) - so its internal min/maxNumberOfTouches
seem to have been set both to 2 - even if UIScrollView
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...
这篇关于UIScrollView只用一根手指滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!