ScrollView在按钮上拖动时不滚动 [英] ScrollView not scrolling when dragging on buttons
问题描述
我有一个滚动视图,用于滚动时,它没有按钮。现在它,并且当拖动鼠标(在模拟器上)没有发生任何事情(我认为,因为按钮被推动)。
I have a scroll view that used to scroll when it didn't have buttons all over it. Now it does, and when dragging the mouse (on simulator) nothing happens (i think because the buttons are being pushed). How can I make this right?
推荐答案
这是因为 UIButton
UIScrollView
的子视图(我假设按钮是作为子视图添加)在跟踪触摸,而不是滚动视图。 UIScrollView
方法 touchesShouldCancelInContentView
是这里的关键。根据其描述:如果视图不是 UIControl
对象,则默认返回值为YES;否则返回 NO
。,即 UIControl
对象(按钮), UIScrollView
不会尝试取消触摸来防止滚动。
This is happening because UIButton
subviews of the UIScrollView
(I assume buttons are added as subviews in your case) are tracking the touches and not the scroll view. UIScrollView
method touchesShouldCancelInContentView
is the key here. According to its description: "The default returned value is YES if view is not a UIControl
object; otherwise, it returns NO
.", i.e. for UIControl
objects (buttons), UIScrollView
does not attempt to cancel touches which prevents scrolling.
因此,允许使用按钮滚动:
So, to allow scrolling with buttons:
- 确保
UIScrollView
属性canCancelContentTouches
设置为YES
。 - 子类
UIScrollView
并覆盖touchesShouldCancelInContentView
以返回YES
当内容视图对象为UIButton
时,如下所示:
- Make sure
UIScrollView
propertycanCancelContentTouches
is set toYES
. - Subclass
UIScrollView
and overridetouchesShouldCancelInContentView
to returnYES
when content view object is aUIButton
, like this:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
if ( [view isKindOfClass:[UIButton class]] ) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}
这篇关于ScrollView在按钮上拖动时不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!