import UIKit class ViewController: UIViewController,UIScrollViewDelegate{ override func viewDidLoad() { super.viewDidLoad() //建立UIScrollView滾動視圖,並設置其顯示大小,位置爲全屏 let scrollView = UIScrollView() //設置delegate爲本身 scrollView.delegate = self //設置UIScrollView顯示內容大小 scrollView.frame = self.view.bounds let imageView = UIImageView(image: UIImage(named: "4")) //設置UIScrollView的內容大小 scrollView.contentSize = imageView.bounds.size //將圖片添加到UIScrollView中 scrollView.addSubview(imageView) //設置UIScrollView的縮放大小(默認縮放小大爲1.0) scrollView.minimumZoomScale = 0.1 scrollView.maximumZoomScale = 4 // 縮放回彈(默認爲true,在超出縮放範圍會有一個回彈效果) scrollView.bouncesZoom = false // 設置是否翻頁 scrollView.isPagingEnabled = false // 顯⽰示⽔水平滾動條,顯⽰示垂直滾動條(默認爲true) scrollView.showsHorizontalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false // 滾動條樣式 scrollView.indicatorStyle = UIScrollView.IndicatorStyle.white // 設置回彈效果(默認爲true) scrollView.bounces = false // 點擊設備狀態欄會自動滾動到頂部 scrollView.scrollsToTop = true self.view.addSubview(scrollView) } //scrollView滾動時調用,只要offset的值發生變化就調用 func scrollViewDidScroll(_ scrollView: UIScrollView) { //打印視圖偏移量 print("X:\(scrollView.contentOffset.x) Y:\(scrollView.contentOffset.y)") } //返回將要縮放的UIView對象。 func viewForZooming(in scrollView: UIScrollView) -> UIView? { for subview:AnyObject in scrollView.subviews{ if subview.isKind(of: UIImageView.self){ return subview as? UIView } } return nil } //當將要開始縮放時,執行該方法。一次有效縮放就只執行一次 func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { } //當scrollView縮放時,調用該方法。在縮放過程當中,會屢次調用 func scrollViewDidZoom(_ scrollView: UIScrollView) { } //當縮放結束後,而且縮放大小回到minimumZoomScale與maximumZoomScale之間後(咱們也許會超出縮放範圍),調用該方法 func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) { } //指示當用戶點擊狀態欄後,滾動視圖是否可以滾動到頂部 func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { return true } //當滾動視圖滾動到最頂端後,執行該方法 func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { } //當開始滾動視圖時,執行該方法。一次有效滑動只執行一次。 func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { } //當開始滾動視圖時,執行該方法。一次有效滑動只執行一次。 func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { } //滑動減速時調用該方法 func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { } //滾動視圖減速完成,滾動將中止時,調用該方法。一次有效滑動只執行一次 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { } //當滾動視圖動畫完成後,調用該方法,若是沒有動畫,那麼該方法將不被調用 func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { } //滑動scrollView,而且手指離開時執行。一次有效滑動只執行一次(當pagingEnabled屬性爲true時,不調用該方法) func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { } }
ViewController:swift
import UIKit class ViewController: UIViewController,UIScrollViewDelegate{ override func viewDidLoad() { super.viewDidLoad() let scrollView = UIScrollView() scrollView.frame = self.view.bounds print(scrollView.bounds.width) scrollView.contentSize = CGSize(width: self.view.bounds.width * 2, height: scrollView.bounds.height) scrollView.isPagingEnabled = true //添加子頁面 let firstView = FirstViewController() //設置圖片播放的頁數 firstView.view.frame = CGRect(x:0, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height) scrollView.addSubview(firstView.view) let secondView = SecondViewController() //設置圖片播放的頁數 secondView.view.frame = CGRect(x: self.view.bounds.width, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height) scrollView.addSubview(secondView.view) self.view.addSubview(scrollView) } }
建立兩個swift文件,命名爲
FirstViewControll,SeconfViewController:ide
import UIKit class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.red } }
兩個文件只有背景顏色設置不同動畫