在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