IOS UIScrollView 滚动视图的使用和文档

岑畅
2023-12-01

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];
属性
名称类型说明默认值
contentOffsetCGPoint滚动视图内容当前滚动的位置0,0
contentSizeCGSize滚动视图内容当前大小
directionalLockEnabledBOOL滚动时候尝试锁定一个方向滚动NO
bouncesBOOL启用回弹效果YES
alwaysBounceVerticalBOOL始终启用垂直回弹效果NO
alwaysBounceHorizontalBOOL始终启用水平回弹效果NO
pagingEnabledBOOL翻页滚动模式NO
scrollEnabledBOOL启动滚动NO
showsVerticalScrollIndicatorBOOL显示垂直滚动条YES
showsHorizontalScrollIndicatorBOOL显示水平滚动条YES
indicatorStyleUIScrollViewIndicatorStyle滚动条样式UIScrollViewIndicatorStyleDefault
indicatorStyleUIScrollViewIndicatorStyle滚动条样式UIScrollViewIndicatorStyleDefault
API
  • - (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 滚动动画时候触发,如果在顶部立即触发。
 类似资料: