关于UIScrollView事件

- touchesShouldBegin:withEvent:inContentView:

Overridden by subclasses to customize the default behavior when a finger touches down in displayed content.

当有一个手指触摸到显示内容上时可以通过子类重写这个默认的行为。

Declaration

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

Parameters

touches

A set of UITouch instances that represent the touches for the starting phase of the event represented by event.

event

An object representing the event to which the touch objects in touches belong.

view

The subview in the content where the touch-down gesture occurred.

Return Value

Return NO if you don’t want the scroll view to send event messages to view. If you want view to receive those messages, return YES (the default).

返回YES(默认)scrollview 不会拦截消息,让这个view接收消息

返回NO scrollview拦截消息 不会将消息传递到参数view上

Discussion

The default behavior of UIScrollView is to invoke the UIResponder event-handling methods of the target subview that the touches occur in.

 

- (BOOL)touchesShouldCancelInContentView:(UIView *)view

Parameters

view

The view object in the content that is being touched. 参数view对象已获取到touched事件

 

Return Value

YES to cancel further touch messages to view, NO to have view continue to receive those messages. 

The default returned value is YES if view is not a UIControl object; otherwise, it returns NO.

返回YES 在这个view上取消进一步的touched消息(不在这个view上处理,事件传到下一个view)。如果这个参数view不是一个UIControl对象,默认返回YES。如果是一个UIControl 对象返回NO.

 

The scroll view calls this method just after it starts sending tracking messages to the content view. If it receives NO from this method, it stops dragging and forwards the touch events to the content subview. The scroll view does not call this method if the value of the canCancelContentTouches property is NO.

 scroll view调用这个函数,在发送taking消息到内容视图之后。如果返回NO,scroll view会停止拖动,触摸事件向子view传递。如果 canCancelContentTouches属性设置为NO 这个函数就不会调用

 

touchesShouldCancelInContentView:

返回YES,表示当前view对这个事件不感兴趣,会传递到其他view

返回NO,scroll view会停止拖动,触摸事件交由该view处理或向子view传递。

 

 

只有scrollView的canCancelContentTouches字段为YES 且 (BOOL)touchesShouldBegin:withEvent:inContentView:返回YES时,这个方法touchesShouldCancelInContentView:才会被调用。

什么情况下touchesShouldBegin返回YES呢? 在点击到scrollView上子视图的时候返回YES,表示让子视图也接收事件

 

总结:

在scroll view上添加一个button当在这个button上滑动的时候,scroll view并不会滑动,因为根据touchesShouldCancelInContentView:的解释该button是一个UIControl对象默认是返回NO。那么怎么能够实现在button上可以滑动scrollview呢?在touchesShouldCancelInContentView的时候返回YES,就可以了。

 

 

 canCancelContentTouches 属性

A Boolean value that controls whether touches in the content view always lead to tracking.

这个属性控制触摸的内容视图是否会跟踪事件

Discussion

If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent:message and the scroll view handles the touch as a scroll. If the value of this property is NO, the scroll view does not scroll regardless of finger movement once the content view starts tracking.


当值是 YES 的时候,用户触碰后,然后在一定时间内没有移动,scrollView 发送 tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,scrollView 发送了 touchesCancelled:withEvent: 到 subview,然后 scroView 开始滚动。假如值是 NO,scrollView 发送 tracking events 后,就算用户移动手指,scrollView 也不会滚动。

 

 

delaysContentTouches Property

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.

 

Discussion

If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately callstouchesShouldBegin:withEvent:inContentView:. The default value is YES.

是个布尔值,当值是 YES 的时候,用户触碰开始,scroll view要延迟一会,看看是否用户有意图滚动。假如滚动了,那么捕捉 touch-down 事件,否则就不捕捉。假如值是NO,当用户触碰, scroll view 会立即触发 touchesShouldBegin:withEvent:inContentView:,默认是 YES

 

 

 

总结:

1、scrollview 先开启一个定时器(150ms)来判断是响应滚动还是子控件的事件(如点击事件)

A:在150ms内有滑动操作,就直接滑动scrollview;如果点击区域是一个UIControl控件,就响应点击事件。

B:用户在touche后150ms内没有操作,就会设置一个traking消息跟踪用户的操作。如果点击是在scrollview的子view上,又分为两个处理:

  (1)、当点击是在UIControl控件区域内 touchesShouldCancelInContentView:返回YES,表示自己处理这个消息并且scrollview不会滚动

  (2)、当点击是在非UIControl控件区域内 touchesShouldCancelInContentView:返回NO, 消息会传递到下一个view,scrollview会响应消息,并且滚动。

 

2、delaysContentTouches属性 :YES 开启一个定时器(150ms)来判断是响应滚动还是子控件的事件; NO 不会开启定时器,直接触发touchesShouldBegin:withEvent:inContentView:

  如果是在UIControl控件上做滑动操作,scrollview是不会滑动的

 

3、 canCancelContentTouches 属性 :是否开启用户跟踪操作,如果设置为NO ,touchesShouldCancelInContentView就不会调用

YES: 移动手指足够长度触发滚动事件

NO: scrollView 发送 tracking events 后,就算用户移动手指,scrollView 也不会滚动。

原文地址:https://www.cnblogs.com/HypeCheng/p/4721884.html