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:滾動(dòng)視圖,是所有滾動(dòng)視圖的基類,只要一個(gè)視圖能夠滾動(dòng),要么是UIScorllview,要么是UIScollView的子類。UIScollView有兩個(gè)很重要子類:UITableView,UICollectionView
//什么時(shí)候才需要滾動(dòng):當(dāng)我們的內(nèi)容區(qū)域大于可視區(qū)域時(shí),為了看到更多內(nèi)容,才需要去滾動(dòng)查看
//創(chuàng)建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)
//設(shè)置scrollView的內(nèi)容區(qū)域大小
scrollView.contentSize = CGSize(width: kScreenWidth*3, height: kScreenHeight*2)
//設(shè)置srollView的偏移量(***)
//? ? ? ? scrollView.contentOffset = CGPoint(x: kScreenWidth, y: 0)
//設(shè)置滾動(dòng)條的樣式
scrollView.indicatorStyle = .white
//設(shè)置是否顯示滾動(dòng)條
//垂直滾動(dòng)條
scrollView.showsVerticalScrollIndicator = false
//水平滾動(dòng)條
scrollView.showsHorizontalScrollIndicator = false
//方向鎖,滾動(dòng)的時(shí)候只能朝一個(gè)方向滾動(dòng)
scrollView.isDirectionalLockEnabled = true
//設(shè)置是否有彈簧效果
//scrollView.bounces = false
//設(shè)置是否總是又水平方向彈簧效果
scrollView.alwaysBounceHorizontal = true
//設(shè)置是否總是又垂直方向彈簧效果
scrollView.alwaysBounceVertical = true
//設(shè)置是否支持整頁(yè)滾動(dòng)
scrollView.isPagingEnabled = true
//設(shè)置scrollView是否支持滾動(dòng)
//? ? ? ? scrollView.isScrollEnabled = false
//設(shè)置scrollView是否支持點(diǎn)擊狀態(tài)欄回到頂部
scrollView.scrollsToTop = true
//scrollView的代理屬性
scrollView.delegate = self
//設(shè)置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的協(xié)議方法extension ViewController : UIScrollViewDelegate{? ? //1 當(dāng)ScorllView滾動(dòng)的時(shí)候會(huì)持續(xù)觸發(fā)? ? func scrollViewDidScroll(_ scrollView: UIScrollView) {? ? ? ? print("滾動(dòng)著,滾動(dòng)著")? ? ? ? print(scrollView.contentOffset)? ? }// any offset changes? ? ? ? //2? 縮放過(guò)程中持續(xù)觸發(fā)? ? 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? 開(kāi)始拖拽的時(shí)候觸發(fā)? ? func scrollViewWillBeginDragging(_ scrollView: UIScrollView){? ? print("開(kāi)始拖拽了")? ? ? ? ? ? }? ? ? ? // 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? 將要結(jié)束拖拽的時(shí)候觸發(fā)? ? func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer){
print("將要結(jié)束拖拽了")
}
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
//5 已經(jīng)結(jié)束拖拽的時(shí)候觸發(fā)
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("結(jié)束拖拽")
}
//6? 將要開(kāi)始減速的時(shí)候觸發(fā)
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView){
print("將要開(kāi)始減速")
}// called on finger up as we are moving
//7? 減速完成,速度為零,這個(gè)方法很重要,往往都是在這個(gè)方法中獲取ScorllView的contentOfSet
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
print("減速完成")
} // called when scroll view grinds to a halt
//8? 給ScorllView設(shè)置一個(gè)結(jié)束動(dòng)畫(huà)的時(shí)候會(huì)觸發(fā),不指定動(dòng)畫(huà)就不會(huì)觸發(fā)
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 將要開(kāi)始縮放的時(shí)候觸發(fā)
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?){
print("將要開(kāi)始縮放的時(shí)候觸發(fā)")
} // called before the scroll view begins zooming its content
//11 結(jié)束縮放的時(shí)候觸發(fā)
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat){
print("結(jié)束縮放的時(shí)候觸發(fā)")
} // scale between minimum and maximum. called after any 'bounce' animations
//12? 設(shè)置點(diǎn)擊狀態(tài)欄是否能回到頂部
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回到頂部觸發(fā)的方法
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){
print("scrollView已經(jīng)回到頂部")
} // called when scrolling
}






