typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
UIScrollViewIndicatorStyleDefault, // black with white border. good against any background
UIScrollViewIndicatorStyleBlack, // black only. smaller. good against a white background
UIScrollViewIndicatorStyleWhite // white only. smaller. good against a black background
};
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
UIScrollViewKeyboardDismissModeNone,
UIScrollViewKeyboardDismissModeOnDrag, // dismisses the keyboard when a drag begins
UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
} NS_ENUM_AVAILABLE_IOS(7_0);
UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateNormal NS_AVAILABLE_IOS(3_0);
UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateFast NS_AVAILABLE_IOS(3_0);
@class UIEvent, UIImageView, UIPanGestureRecognizer, UIPinchGestureRecognizer;
@protocol UIScrollViewDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> {
@package
id _delegate;
}
@property(nonatomic) CGPoint contentOffset; // 表示scrollview的偏移量
@property(nonatomic) CGSize contentSize; // scrollview的滚动范围
@property(nonatomic) UIEdgeInsets contentInset; // contentview四周的扩展大小,相当于改变了contentview的大小
@property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // default nil. weak reference
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled; // 指定空间只能在一个反向上滚动
@property(nonatomic) BOOL bounces; //控件遇到边框是否反弹
@property(nonatomic) BOOL alwaysBounceVertical; // 控件垂直反向遇到边框是否反弹
@property(nonatomic) BOOL alwaysBounceHorizontal; // 控件水平反向遇到边框是否反弹
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled; // 控件是否整页滚动
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; // 控件是否能滚动
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // 控件是否显示水平滚动条
@property(nonatomic) BOOL showsVerticalScrollIndicator; // 控件是否显示垂直滚动条
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets; // 指定滚动条在scrollview中的位置
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // 设定滚动条的样式
@property(nonatomic) CGFloat decelerationRate NS_AVAILABLE_IOS(3_0);改变scrollview的速度点位置
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; // animate at constant velocity to new offset
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // scroll so rect is just visible (nearest edges). nothing if rect completely visible
- (void)flashScrollIndicators; // displays the scroll indicators for a short time. This should be done whenever you bring the scroll view to front.
/*
Scrolling with no scroll bars is a bit complex. on touch down, we don't know if the user will want to scroll or track a subview like a control.
on touch down, we start a timer and also look at any movement. if the time elapses without sufficient change in position, we start sending events to
the hit view in the content subview. if the user then drags far enough, we switch back to dragging and cancel any tracking in the subview.
the methods below are called by the scroll view and give subclasses override points to add in custom behaviour.
you can remove the delay in delivery of touchesBegan:withEvent: to subviews by setting delaysContentTouches to NO.
*/
@property(nonatomic,readonly,getter=isTracking) BOOL tracking; // 监控当前目标是否被跟踪
@property(nonatomic,readonly,getter=isDragging) BOOL dragging; // 监控当前目标是否被拖拽
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating; // 监控当前目标是否正在减速
@property(nonatomic) BOOL delaysContentTouches; // 控制视图是否延迟调用开始滚动的方法
@property(nonatomic) BOOL canCancelContentTouches; // 控制控件是否接触取消touch事件
// 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; 决定自己是否可以接受touch事件
// 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; 开始发送 tracking messages 消息给subview 的时候调用这个方法,决定是否发送 tracking messages 消息到subview。假如返回NO,发送。YES 则不发送。
假如 canCancelContentTouches属性是NO,则不调用这个方法来影响如何处理滚动手势
/*
the following properties and methods are for zooming. as the user tracks with two fingers, we adjust the offset and the scale of the content. When the gesture ends, you should update the content
as necessary. Note that the gesture can end and a finger could still be down. While the gesture is in progress, we do not send any tracking calls to the subview.
the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale: in order for zooming to work and the max/min zoom scale must be different
note that we are not scaling the actual scroll view but the 'content view' returned by the delegate. the delegate must return a subview, not the scroll view itself, from viewForZoomingInScrollview:
*/
@property(nonatomic) CGFloat minimumZoomScale; // 缩小的最小比例
@property(nonatomic) CGFloat maximumZoomScale; // 放大的最大比例
@property(nonatomic) CGFloat zoomScale NS_AVAILABLE_IOS(3_0); // 设置变化比例
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
@property(nonatomic) BOOL bouncesZoom; // 控制缩放的时候是否反弹
@property(nonatomic,readonly,getter=isZooming) BOOL zooming; //判断控件的大小是否正在被改变
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing; // 判断是否正在进行缩放反弹
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
@property(nonatomic) BOOL scrollsToTop; // 滚动到顶部
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);按下手势
// `pinchGestureRecognizer` will return nil when zooming is disabled.
@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);捏和的手势
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); // default is UIScrollViewKeyboardDismissModeNone
设置键盘的消失模式
@end
@protocol UIScrollViewDelegate<NSObject>
@optional
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes视图已经开始滑动所触发的方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes视图开始缩放触发的方法
// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;视图开始拽动所触发的方法
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
视图拖动结束所触发的方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving视图开始减速时所触发的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // called when scroll view grinds to a halt视图结束减速时所触发的方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
视图动画结束时触发的方法,使用set方法设置偏移量后回触发
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // return a view that will be scaled. if delegate returns nil, nothing happens返回进行缩放的视图
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content
视图内容将要开始缩放时触发的方法
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations
视图内容结束缩放时触发的方法
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; // return a yes if you want to scroll to the top. if not defined, assumes YES返回yes,开启快捷滚动回顶端,将要滚动时调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView; // called when scrolling animation finished. may be called immediately if already at top视图快捷滚动回顶端开始动作时调用
补充几点:
从你的手指touch屏幕开始,scrollView开始一个timer,如果:
1. 150ms内如果你的手指没有任何动作,消息就会传给subView。
2. 150ms内手指有明显的滑动(一个swipe动作),scrollView就会滚动,消息不会传给subView,这里就是产生问题二的原因。
3. 150ms内手指没有滑动,scrollView将消息传给subView,但是之后手指开始滑动,scrollView传送touchesCancelled消息给subView,然后开始滚动。
观察下tableView的情况,你先按住一个cell,cell开始高亮,手不要放开,开始滑动,tableView开始滚动,高亮取消。
delaysContentTouches的作用:
这个标志默认是YES,使用上面的150ms的timer,如果设置为NO,touch事件立即传递给subView,不会有150ms的等待。
cancelsTouches的作用:
这个标准默认为YES,如果设置为NO,这消息一旦传递给subView,这scroll事件不会再发生。