import UIKit
class ViewController: UIViewController {
//获取屏幕的宽
let kScreenWidth = UIScreen.main.bounds.size.width
//获取屏幕的高
let kScreenHeight = UIScreen.main.bounds.size.height
override func viewDidLoad() {
super.viewDidLoad()
//UIScrollView:滚动视图,是所有滚动视图的基类,只要一个视图能够滚动,要么是UIScorllview,要么是UIScollView的子类。UIScollView有两个很重要子类:UITableView,UICollectionView
//什么时候才需要滚动:当我们的内容区域大于可视区域时,为了看到更多内容,才需要去滚动查看
//创建UIScrollView
let scrollView = UIScrollView(frame:CGRect(x: 20, y: 20, width: kScreenWidth - 40, height: kScreenHeight - 40))
scrollView.backgroundColor = #colorLiteral(red: 1, green: 0.4710629376, blue: 0.4618466702, alpha: 1)
//设置scrollView的内容区域大小
scrollView.contentSize = CGSize(width: kScreenWidth*3, height: kScreenHeight*2)
//设置srollView的偏移量(***)
// scrollView.contentOffset = CGPoint(x: kScreenWidth, y: 0)
//设置滚动条的样式
scrollView.indicatorStyle = .white
//设置是否显示滚动条
//垂直滚动条
scrollView.showsVerticalScrollIndicator = false
//水平滚动条
scrollView.showsHorizontalScrollIndicator = false
//方向锁,滚动的时候只能朝一个方向滚动
scrollView.isDirectionalLockEnabled = true
//设置是否有弹簧效果
//scrollView.bounces = false
//设置是否总是又水平方向弹簧效果
scrollView.alwaysBounceHorizontal = true
//设置是否总是又垂直方向弹簧效果
scrollView.alwaysBounceVertical = true
//设置是否支持整页滚动
scrollView.isPagingEnabled = true
//设置scrollView是否支持滚动
// scrollView.isScrollEnabled = false
//设置scrollView是否支持点击状态栏回到顶部
scrollView.scrollsToTop = true
//scrollView的代理属性
scrollView.delegate = self
//设置scrollView最大最小缩放比例
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3
let imageView = UIImageView(frame:CGRect(x: 0, y: 0, width: kScreenWidth - 40, height: kScreenHeight - 40))
imageView.tag = 200
imageView.image = UIImage(named: "a.jpg")
scrollView.addSubview(imageView)
self.view.addSubview(scrollView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//在延展中管理UIScrollViewDelegate的协议方法extension ViewController : UIScrollViewDelegate{ //1 当ScorllView滚动的时候会持续触发 func scrollViewDidScroll(_ scrollView: UIScrollView) { print("滚动着,滚动着") print(scrollView.contentOffset) }// any offset changes //2 缩放过程中持续触发 func scrollViewDidZoom(_ scrollView: UIScrollView){ print("缩放着缩放着") print(scrollView.zoomScale) }// any zoom scale changes // called on start of dragging (may require some time and or distance to move) //3 开始拖拽的时候触发 func scrollViewWillBeginDragging(_ scrollView: UIScrollView){ print("开始拖拽了") } // 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 //4 将要结束拖拽的时候触发 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer){
print("将要结束拖拽了")
}
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
//5 已经结束拖拽的时候触发
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("结束拖拽")
}
//6 将要开始减速的时候触发
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView){
print("将要开始减速")
}// called on finger up as we are moving
//7 减速完成,速度为零,这个方法很重要,往往都是在这个方法中获取ScorllView的contentOfSet
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
print("减速完成")
} // called when scroll view grinds to a halt
//8 给ScorllView设置一个结束动画的时候会触发,不指定动画就不会触发
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView){
} // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//9 返回ScorllView上缩放的视图
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return scrollView.viewWithTag(200)
}// return a view that will be scaled. if delegate returns nil, nothing happens
//10 将要开始缩放的时候触发
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?){
print("将要开始缩放的时候触发")
} // called before the scroll view begins zooming its content
//11 结束缩放的时候触发
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat){
print("结束缩放的时候触发")
} // scale between minimum and maximum. called after any 'bounce' animations
//12 设置点击状态栏是否能回到顶部
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
return true
}// return a yes if you want to scroll to the top. if not defined, assumes YES
//13 scrollView回到顶部触发的方法
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){
print("scrollView已经回到顶部")
} // called when scrolling
}