iOS-UIKit框架学习—UIScrollView

UIScrollView的类提供支持显示的内容是大于应用程序的窗口大小。它可以使用户内滚动的内容,通过刷卡手势,和捏的手势放大和部分内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@class UIEvent, UIImageView, UIPanGestureRecognizer, UIPinchGestureRecognizer;
@protocol UIScrollViewDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding>
// 内容视图原点与滚动视图原点的偏移值,默认值为0
@property(nonatomic) CGPoint contentOffset;
// 内容视图的大小,默认值为0
@property(nonatomic) CGSize contentSize;
// 内容视图额外的滚动范围,默认值为UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets contentInset;
// 代理
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate;
// 是否锁定视图垂直或水平方向滚动,默认为NO
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;
// 拖动到屏幕的边缘是否弹跳效果,默认值为YES
@property(nonatomic) BOOL bounces;
// 是否总是允许垂直方向拉伸拖动,默认值为YES
@property(nonatomic) BOOL alwaysBounceVertical;
// 是否总是允许水平方向拉伸拖动,默认值为YES
@property(nonatomic) BOOL alwaysBounceHorizontal;
// 是否支持翻页,默认值为NO
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;
// 是否开启拖动效果,默认值是YES
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
// 水平方向滚动指示器是否可见,默认值为YES
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
// 垂直方向滚动指示器是否可见,默认值为YES
@property(nonatomic) BOOL showsVerticalScrollIndicator;
// 滚动指示器的间距范围,默认值为UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets;
// 滚动指示器的风格
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle;
// 当手指离开屏幕滚动的减速率
// UIScrollViewDecelerationRateNormal and UIScrollViewDecelerationRateFast
@property(nonatomic) CGFloat decelerationRate;
// 设置内容的偏移量。与contentOffset属性设置有区别,如:关闭动画效果没有抖动
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
// 设置在指定滚动区域可见
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
// 短暂的显示滚动指示器
- (void)flashScrollIndicators;
// 获取用户是否触摸了滚动视图
@property(nonatomic,readonly,getter=isTracking) BOOL tracking;
// 获取用户是否应经滚动视图
@property(nonatomic,readonly,getter=isDragging) BOOL dragging;
// 获取用户手指离开视图后是否在滚动
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;
// 是否延迟处理触摸手势,默认值为YES
@property(nonatomic) BOOL delaysContentTouches;
// 是否总是监听滚动视图中触摸事件,默认值为YES
@property(nonatomic) BOOL canCancelContentTouches;
// 由子类重写自定义默认的行为,当手指触摸到显示的内容。默认值为YES
- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event inContentView:(UIView *)view;
// 返回是否取消涉及相关内容的子视图并开始拖放
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
// 滚动视图内容可拖放的最大比例,默认值为1
@property(nonatomic) CGFloat minimumZoomScale;
// 滚动视图内容可拖放的最小比例,默认值为1
@property(nonatomic) CGFloat maximumZoomScale;
// 指定当前公洞视图的拖放比例,默认值为1
@property(nonatomic) CGFloat zoomScale NS_AVAILABLE_IOS(3_0);
// 新的值应该在是minimumZoomScale和maximumZoomScale之间
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
// 设定特定区域内的视图可见
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
// 确定视图动画的缩放大小是否超过最大或最小缩放限制,默认值为YES
@property(nonatomic) BOOL bouncesZoom;
// 是否在使用缩放手势
@property(nonatomic,readonly,getter=isZooming) BOOL zooming;
// 是否正在缩放回最大或最小值
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing;
// 是否启用滚动到顶部的手势,默认值为YES
@property(nonatomic) BOOL scrollsToTop __TVOS_PROHIBITED;
// 点击手势
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);
// 捏合手势
@property(nullable, nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);
// 按压手势
@property(nonatomic, readonly) UIGestureRecognizer *directionalPressGestureRecognizer UIKIT_AVAILABLE_TVOS_ONLY(9_0);
// 键盘显示方式
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); // default is UIScrollViewKeyboardDismissModeNone
// 属性控制器
@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(10_0) __TVOS_PROHIBITED;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@protocol UIScrollViewDelegate<NSObject>
@optional
// 当用户滚动内容视图事调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// 滚动视图的缩放比例发生改变时调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2);
// 开始拖动视图时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// 当拖动内容滚动视图将要完成时调用
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
// 告诉代理拖动内容滚动视图结束
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// 到拖动结束滚动视图正在减速时调用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
// 减速效果结束调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
// 当滚动视图的滚动动画结束时调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
// 返回按比例缩放视图,当缩放将要发生时调用
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
// 当缩放滚动视图的内容即将展开时调用
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2);
// 滚动视图的缩放完成
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale;
// 询问是否将要返回到视图顶部时调用
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
// 返回到视图顶部时滚动动画完成后调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
// 滚动指示器的风格
typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
UIScrollViewIndicatorStyleDefault, // 默认风格、适用任何背景
UIScrollViewIndicatorStyleBlack, // 黑色, 适用白色背景
UIScrollViewIndicatorStyleWhite // 白色
};
// 当开始拖动scroll view时,键盘的消失方式
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
UIScrollViewKeyboardDismissModeNone, // 无
UIScrollViewKeyboardDismissModeOnDrag, // 拖动时,关闭键盘
UIScrollViewKeyboardDismissModeInteractive, // 随着scroll view向下滚动,键盘随着一起消失
} NS_ENUM_AVAILABLE_IOS(7_0);