IOS8下的UIScrollView有子控件时滑动经常不能滚动解决办法

在ios8下运行以前开发的项目时,发现UIScrollView经常不能滑动,测试后发现如果触摸点是在子控件下,则会导致触摸事件被子控件接收后不能再次返回事件给UIScrollView,以至于不能滑动UIScrollView。

翻阅资料,得知:

UIScrollView在得到用户的触摸事件后,会有150ms的延迟响应,在这150ms内,如果被判定为滑动UIScrollView,则不传递事件给子控件,反之则传递事件到其子控件。而在子控件中,当得到传递来的触摸事件后,如果用户再次滑动屏幕,则其子控件会再次把事件传递到其父控件UIScrollView。


所以,我们可以判定,导致UIScrollView不能正常滑动的原因是子控件不能正常把触摸滑动事件返还给UIScrollView。


我们再来看看下面:

UIScrollView中有两个属性和两个方法

@property(nonatomic) BOOL delaysContentTouches;       // default is YES. if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:
@property(nonatomic) BOOL canCancelContentTouches;    // default is YES. if NO, then once we start tracking, we don't try to drag if the touch moves

// override points for subclasses to control delivery of touch events to subviews of the scroll view
// called before touches are delivered to a subview of the scroll view. if it returns NO the touches will not be delivered to the subview
// default returns YES
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;
// called before scrolling begins if touches have already been delivered to a subview of the scroll view. if it returns NO the touches will continue to be delivered to the subview and scrolling will not occur
// not called if canCancelContentTouches is NO. default returns YES if view isn't a UIControl
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;

属性delaysContentTouches设置UIScrollView在得到触摸事件后是否延迟150ms后再调用方法touchesShouldBegin:withEvent:inContentView:;

属性canCancelContentTouches设置是否能从子控件把滑动事件重新返回给父控件UIScrollView,如果为NO将不调用方法touchesShouldCancelInContentView:;


方法touchesShouldBegin:withEvent:inContentView:作用为是否把事件传递到子控件。

方法touchesShouldCancelInContentView:作用为是否允许从子控件中把触摸滑动事件返还给父控件UIScrollView。



由此,我们只需重写方法touchesShouldCancelInContentView:并让其返回YES,即可解决UIScrollView不能滑动的问题。


在此感谢一下下面这篇博文的博主:http://blog.csdn.net/onlyou930/article/details/8198862


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值