import UIKitclass ViewController: UIViewController {? ? //屏幕的寬? ? let kscreenwidth = UIScreen.main.bounds.size.width? ? //屏幕的高? ? let kscreenheight = UIScreen.main.bounds.size.height? ? override func viewDidLoad() {? ? ? ? super.viewDidLoad()? ? ? //UIScrollView:滾動視圖,是所有滾動視圖的基類,只要一個視圖能夠滾動馁龟,要么是UIScrollView,要是UIScrollView的子類,UIScrollView有兩個很重要子類:UITableView,UICollectionView? ? ? //什么時候才需要滾動:當我們的內容大于可視區(qū)域時寸莫,為了看到更多內容尖滚,才需要滾動去查看? ? ? ? //創(chuàng)建UIScrollView? ? ? ? let scrollview = UIScrollView(frame: CGRect(x: 20, y: 20, width: kscreenwidth - 40, height: kscreenheight - 40))? ? ? ? scrollview.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)? ? ? ? //設置scrollview的內容區(qū)域大小? ? ? ? scrollview.contentSize = CGSize(width: 3*kscreenwidth, height: 2*kscreenheight)? ? ? ? ? ? ? ? ? ? ? ? let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 3*kscreenwidth, height: 2*kscreenheight))? ? ? ? imageview.image = #imageLiteral(resourceName: "image.jpg")? ? ? ? //設置scrollview的偏移量? ? ? ? //scrollview.contentOffset = CGPoint(x: kscreenwidth, y: 0)? ? ? ? scrollview.addSubview(imageview)? ? ? ? //設置滾動條樣式? ? ? ? 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? ? ? ? self.view.addSubview(scrollview)? ? }? ? ? ? ? ? }? ? //在延展中管理UIScrollViewDelegate的協(xié)議方法? ? extension ViewController:UIScrollViewDelegate{? ? ? //1當scrollView滾動的時候這個方法會持續(xù)觸發(fā)? ? ? ? func scrollViewDidScroll(_ scrollView: UIScrollView) {// any offset changes? ? ? ? print("滾動著,滾動著")? ? ? ? ? ? print(scrollView.contentOffset)? ? ? ? }? ? ? ? //2縮放的過程中持續(xù)觸發(fā)? ? ? ? func scrollViewDidZoom(_ scrollView: UIScrollView){ // any zoom scale changes? ? ? ? // called on start of dragging (may require some time and or distance to move)? ? ? ? ? ? print("縮放著")? ? ? ? ? ? print(scrollView.zoomScale)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? //3開始拖拽的時候觸發(fā)? ? ? ? func scrollViewWillBeginDragging(_ scrollView: UIScrollView)? ? ? ? ? ? ? ? {// 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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("開始拖拽了")? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? //4將要結束拖拽的時候觸發(fā)? ? ? ? func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer)
{// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
print("要結束拖拽了")
}
//5已經(jīng)結束拖拽的時候觸發(fā)
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("結束拖拽")
}
//6將要開始減速的時候觸發(fā)
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView){ // called on finger up as we are moving
print("將要開始減速")
}
//7減速完成庐氮,速度為0语稠,這個方法很重要,往往都是在這個方法中獲取scrollView偏量
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {// called when scroll view grinds to a halt
print("減速完成")
}
//8給scrollView設置一個結束動畫的時候觸發(fā),不制定動畫就不會觸發(fā)
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
}
//9返回scrollView上縮放視圖
func viewForZooming(in scrollView: UIScrollView) -> UIView? {// return a view that will be scaled. if delegate returns nil, nothing happens
return scrollView.viewWithTag(200)
}
//10將要開始縮放的時候觸發(fā)
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {// called before the scroll view begins zooming its content
print("將要開始縮放的時候觸發(fā)")
}
//11結束縮放的時候觸發(fā)
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {// scale between minimum and maximum. called after any 'bounce' animations
print("結束縮放的時候觸發(fā)")
}
//12設置點就狀態(tài)欄是否能回到頂部
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {// return a yes if you want to scroll to the top. if not defined, assumes YES
return true
}
//13scrollView回到頂部的觸發(fā)的方法
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){ // called when scrolling animation finished. may be called immediately if already at top
print("scrollView已經(jīng)回到頂部")
}
}
// override func didReceiveMemoryWarning() {
// super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// }
//}