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;
@interface UIScrollView : UIView <NSCoding> {
【创建相关】
@property(nonatomic) CGPoint contentOffset; // 内容的左上角坐标
@property(nonatomic) CGSize contentSize; // 内容大小(旧版用于布局,新版直接用自动布局)
@property(nonatomic) UIEdgeInsets contentInset; // 内容边缘
scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30, 40);
[scrollView setContentOffset:CGPointMake(100, 0)];//设置滚动到的位置。
绝对布局
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300, self.view.bounds.size.width, 100)];
scrollView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width*2, 100);//内容大小。
imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
imgView1.backgroundColor = [UIColor redColor];
[scrollView addSubview:imgView1];
imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, 100)];
imgView2.backgroundColor = [UIColor greenColor];
[scrollView addSubview:imgView2];
自动布局
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.mas_equalTo(self.view);
make.top.mas_equalTo(self.view).mas_offset(300);
make.height.mas_equalTo(200);
}];
[imgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo(scrollView);
make.width.mas_equalTo(self.view);
make.height.mas_equalTo(scrollView);
make.bottom.mas_equalTo(scrollView);
}];
[imgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.right.mas_equalTo(scrollView);
make.width.mas_equalTo(self.view);
make.height.mas_equalTo(scrollView);
make.left.mas_equalTo(imgView1.mas_right);
make.bottom.mas_equalTo(scrollView);
}];
【常用属性】
@property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 具体查看代理内容
@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; // 指定滚动条的位置
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // 滚动条的样式
@property(nonatomic) CGFloat decelerationRate; // 减速值域(0,1)手指离开后减速的快慢。
//滚动条样式
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
};
【滚动与滚动条】
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; // 滚动到指定位置
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // 指定rect,滚动到其完全显示
- (void)flashScrollIndicators; // 显示滚动条一小段事件
[scrollView flashScrollIndicators];
[scrollView setContentOffset:CGPointMake(100, 0) animated:YES];
[scrollView scrollRectToVisible:CGRectMake(self.view.bounds.size.width, 0, 31, 100) animated:YES];
【触摸滚动】
//结合delegate
@property(nonatomic,readonly,getter=isTracking) BOOL tracking; // 如果用户已经触及的内容视图,但可能还没有开始拖动它。则YES
@property(nonatomic,readonly,getter=isDragging) BOOL dragging; // 用户拖动一小会或一小段距离,则YES
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating; // 用户已经不触及内容,但还是在拖动,则YES
@property(nonatomic) BOOL delaysContentTouches; // 是否延迟处理:默认是YES,if NO 立即调用 touchesShouldBegin:。。(防止一摸就滚动)
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;
@property(nonatomic) BOOL canCancelContentTouches; // 默认是YES. if NO 移动就出发滚动。(??)
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
【放大缩小】
//结合delegate
@property(nonatomic) CGFloat minimumZoomScale; // default is 1.0
@property(nonatomic) CGFloat maximumZoomScale; // default is 1.0. must be > minimum zoom scale to enable zooming
@property(nonatomic) CGFloat zoomScale NS_AVAILABLE_IOS(3_0); // default is 1.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; // 一个布尔值,指示缩放已超过指定的接收范围
【其他】
@property(nonatomic) BOOL scrollsToTop; // default is YES.
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);
@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0);
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);
@end