UIScrollView 是一个可滚动的视图列表,在app中非常常见。下面简单的介绍一下UIScrollView 的用法。可实现代理UIScrollViewDelegate
// 实例化UIScrollView
UIScrollView* _scrollView = [[UIScrollView alloc] init];
// 获取屏幕大小
UIScreen* screen = [UIScreen mainScreen];
// 设置滚动视图大小
_scrollView.frame = CGRectMake(0, 0, screen.bounds.size.width, screen.bounds.size.height);
// 设置滚动视图内容画布大小,当内容画布大小超过frame时候会出现滚动
_scrollView.contentSize = CGSizeMake(screen.bounds.size.width, maxViewNum * screen.bounds.size.height);
// 设置回弹效果
_scrollView.bounces = YES;
// 设置可滚动
_scrollView.scrollEnabled = YES;
// 设置显示Y轴滚地图
_scrollView.showsVerticalScrollIndicator = YES;
[self.view addSubview:_scrollView];
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
contentOffset | CGPoint | 滚动视图内容当前滚动的位置 | 0,0 |
contentSize | CGSize | 滚动视图内容当前大小 | |
directionalLockEnabled | BOOL | 滚动时候尝试锁定一个方向滚动 | NO |
bounces | BOOL | 启用回弹效果 | YES |
alwaysBounceVertical | BOOL | 始终启用垂直回弹效果 | NO |
alwaysBounceHorizontal | BOOL | 始终启用水平回弹效果 | NO |
pagingEnabled | BOOL | 翻页滚动模式 | NO |
scrollEnabled | BOOL | 启动滚动 | NO |
showsVerticalScrollIndicator | BOOL | 显示垂直滚动条 | YES |
showsHorizontalScrollIndicator | BOOL | 显示水平滚动条 | YES |
indicatorStyle | UIScrollViewIndicatorStyle | 滚动条样式 | UIScrollViewIndicatorStyleDefault |
indicatorStyle | UIScrollViewIndicatorStyle | 滚动条样式 | UIScrollViewIndicatorStyleDefault |
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
使用动画设置滚动位置- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
使用动画滚动到指定矩形区域- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated
动画设置缩放- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
动画移动到指定矩形区域- (void)scrollViewDidScroll:(UIScrollView *)scrollView
视图发生滚动时触发- (void)scrollViewDidZoom:(UIScrollView *)scrollView
视图发生放大触发- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
视图开始拖动时候触发- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
拖动结束触发- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
减速时候触发,decelerate 表示减速后会继续减速。- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
手指抬起时- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
视图停止滚动时候触发- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
setContentOffset执行完后触发,如果没有动画则不触发。- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view
视图即将缩放时候触发- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
返回一个YES返回到顶部- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
滚动动画时候触发,如果在顶部立即触发。