概述
UIScrollView
允许滚动和缩放其包含的视图的视图。
属性和方法
初始化方法
let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height))
设置偏移量
scrollView.contentSize = CGSize.init(width: view.bounds.size.width, height: 1150)
设置视图是否支持滑动到最顶端
scrollView.scrollsToTop = true
设置是否反弹
scrollView.bounces = true
设置是否分页
scrollView.isPagingEnabled = true
设置是否可以滚动
scrollView.isScrollEnabled = true
设置是否显示滚动条
scrollView.showsVerticalScrollIndicator = true
设置滚动条的样式
scrollView.indicatorStyle = .white
代理方法
当视图开始滚动时执行此代理方法
optional public func scrollViewDidScroll(_ scrollView: UIScrollView) // any offset changes
当视图即将开始被拖拽时执行的代理方法
optional public func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
当视图结束拖拽时执行的代理方法
optional public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
当视图减速停止时执行的代理方法
optional public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) // called when scroll view grinds to a halt
当视图开始减速时执行的代理方法
optional public func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) // called on finger up as we are moving
是否支持滑动到顶部
optional public func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool // return a yes if you want to scroll to the top. if not defined, assumes YES
视图已经放大或缩小
optional public func scrollViewDidZoom(_ scrollView: UIScrollView) // any zoom scale changes
缩放结束时执行的代理方法
optional public func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) // scale between minimum and maximum. called after any 'bounce' animations
视图开始放大或者缩小时执行的代理方法
optional public func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) // called before the scroll view begins zooming its content