ScrollView not scrolling when dragging on buttons(拖动按钮时ScrollView不滚动)
问题描述
我有一个滚动视图,当它上面没有按钮时会滚动.现在确实如此,并且当拖动鼠标(在模拟器上)时,什么也没有发生(我认为是因为按钮被按下了).我怎样才能做到这一点?
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?
推荐答案
发生这种情况是因为 UIScrollView 的 UIButton 子视图(我假设按钮作为子视图添加到您的case) 正在跟踪触摸而不是滚动视图.UIScrollView 方法 touchesShouldCancelInContentView 是这里的关键.根据其描述:如果view不是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以在内容视图对象是UIButton时返回YES,如下所示:
- Make sure
UIScrollViewpropertycanCancelContentTouchesis set toYES. - Subclass
UIScrollViewand overridetouchesShouldCancelInContentViewto returnYESwhen content view object is aUIButton, like this:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
if ( [view isKindOfClass:[UIButton class]] ) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}
这篇关于拖动按钮时ScrollView不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:拖动按钮时ScrollView不滚动
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
