問題描述
iOS7 &iOS8
iOS7 & iOS8
我需要在 UIScrollview
中禁用兩指或三指滾動.
I need to disable 2 or three fingers scrolling in UIScrollview
.
我試過了:
[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];
但它沒有效果.仍然可以用兩根手指滾動.
But it has no effect. It is still possible to scroll with 2 fingers.
如果我嘗試將 max 和 min 設置為 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 ?
謝謝.
推薦答案
問題:
當 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!
解決方案:
將 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;
在滾動視圖的@interface 聲明中符合 UIGestureRecognizerDelegate
協議.您不必為 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;
}
}
}
<小時>
更安全的版本:
如果您有一個自定義的 scrollView 類并且想要非常安全,您還可以再添加一行代碼來消除與其他 scrollView 的歧義:
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;
}
}
<小時>
附加信息:
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;
<小時>
極客部分:
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;
}
}
這是訪問負責 UIPageViewController
分頁的底層滾動視圖的方法.將此代碼放在例如UIPageViewController
(自身)的viewDidLoad:
.
如果您根本無法訪問滾動視圖 - 就像動態 UITableViewCell
中的 UIPageViewController
在運行時發生創建和單元重用并且沒有插座可以在其 contentViews 上設置 - 在 UIScrollView
上放置一個類別并在那里覆蓋委托方法.不過要小心!這會影響您的應用程序中的每個滾動視圖 - 所以要像上面的更安全"示例中那樣進行適當的自省(類檢查)...... ;-)
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... ;-)
旁注:
不幸的是,同樣的技巧不適用于 UIScrollView
上的 pinchGestureRecognizer
因為它不公開 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 只能用一根手指滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!