[关闭]
@chenbinghua 2015-10-10T12:03:32.000000Z 字数 2005 阅读 1472

iOS开发之UIScrollView

iOS笔记


什么是UIScrollView

UIScrollView是iOS开发中最常用的控件之一,是UITableViewUICollectionView的直接父类。

UIScrollView无法滚动的可能原因

UIScrollView的三个常见属性

查看UIScrollView的头文件

  1. @property(nonatomic) CGPoint contentOffset; // default CGPointZero
  2. @property(nonatomic) CGSize contentSize; // default CGSizeZero
  3. @property(nonatomic) UIEdgeInsets contentInset; // default UIEdgeInsetsZero. add additional scroll area around content

contentOffset这个属性用来表示UIScrollView滚动的位置
(其实就是内容左上角与scrollView左上角的间距值)
contentSize这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远)
contentInset这个属性能够在UIScrollView的4周增加额外的滚动区域,一般用来避免scrollView的内容被其他控件挡住

此处输入图片的描述

UIScrollView的其他属性

  1. @property(nonatomic) BOOL bounces;
  2. // 设置UIScrollView是否需要弹簧效果
  3. @property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
  4. // 设置UIScrollView是否能滚动
  5. @property(nonatomic) BOOL showsHorizontalScrollIndicator;
  6. // 是否显示水平滚动条
  7. @property(nonatomic) BOOL showsVerticalScrollIndicator;
  8. // 是否显示垂直滚动条

UIScrollView的代理

打开UIScrollViewDelegate的头文件,可以看到一堆代理方法

  1. 常用的代理方法有
  2. - (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes
  3. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
  4. // 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
  5. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
  6. // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
  7. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
  8. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving
  9. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // called when scroll view grinds to a halt
  10. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating

UIScrollView的其他用法

内容缩放
分页

取消系统自动调节

self.automaticallyAdjustsScrollViewInsets = NO;

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注