首页 > 代码库 > UIScrollView笔记
UIScrollView笔记
UIScrollView必须知道它的contentsize
这就是为啥TableView的row的height必须被计算,即使那些row还没有出现在屏幕。The scroll view must know the size of the content view so it knows when to stop scrolling; by default, it “bounces” back when scrolling exceeds the bounds of the content
为了确定用户是在点击还是在滑动,UIScrollView自己维护了一个计时器,并记录手指的位置。如果计时器相应之前,还没有比较显著的移动,那么UIScrollView就发送触摸时间到相应的subview;否则就滑动自己。
scrollsToTop
当用户轻点状态栏时,系统会要求离状态栏近的scrollview把自己的内容滑到最上面。如果这个属性是false,就会忽略这个请求。On iPhone, the scroll-to-top gesture has no effect if there is more than one scroll view on-screen that has scrollsToTop set to true.
设置特定区域可见
scrollRectToVisible(_ rect: CGRect, animated: Bool)
设置分页
scrollView.pagingEnabled = true
当停止滑动时,会停到scrollview的大小的整数倍
设置方向锁定。
一般来说,scrollview可以在横向、纵向滑动。设定锁定以后,就只能在一个方向锁定了。在哪个方向锁定要看在哪个方向移动的距离大。scrollView.directionalLockEnabled = true
Scroll Indicator
就是滑动时出现的那条线scrollView.indicatorStyle = .Black scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 5, 10, 30) button.addTarget(self, action: #selector(ViewController.flashIndicator(_:)), forControlEvents: .TouchUpInside)
func flashIndicator(sender:UIButton) -> Void { scrollView.flashScrollIndicators() }
和键盘的交互
var keyboardDismissMode: UIScrollViewKeyboardDismissMode { get set
- none
- onDrag:
drag开始时就消失 interactive
The keyboard follows the dragging touch offscreen, and can be pulled upward again to cancel the dismiss。
UIScrollViewDelegate调用顺序
- 滑动,然后放开
scrollViewWillBeginDragging
几次scrollViewDidScroll
scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)
scrollViewDidEndDragging(_:willDecelerate:)
scrollViewWillBeginDecelerating
若干scrollViewDidScroll
scrollViewDidEndDecelerating - 滑动,scrollview不动,然后放开
scrollViewWillBeginDragging
几次scrollViewDidScroll
scrollViewWillEndDragging
scrollViewDidEndDragging
- 滑动,然后放开
isDragging, isTracking
- 滑动,然后放开
在scrollViewWillBeginDecelerating
之后,istracking变为false
在scrollViewDidEndDecelerating
之前,isdragging为true - 滑动,scrollview不动,然后放开
isTrackiing一直是true isdragging在scrollViewWillEndDragging
之前是true
- 滑动,然后放开
UIScrollView笔记