GitHub前端
class ViewController: UIViewController,UIScrollViewDelegate { var scrollView = UIScrollView() var imageView = UIImageView() var image = UIImage() override func viewDidLoad() { super.viewDidLoad() self.image = UIImage.init(named: "test")! self.imageView.image = self.image self.view.backgroundColor = UIColor.green self.view.addSubview(self.scrollView)// 將ScrollView添加到視圖上 let width = self.view.bounds.size.width * 2 let height = self.view.bounds.size.height * 2 self.scrollView.frame = self.view.bounds // 設置scrollView的 frame self.scrollView.contentSize = CGSize(width:width, height:height) //設置scrollView的 contentSize self.scrollView.delegate = self // 設置scrollView的 代理 print(self.scrollView.contentOffset) // scrollView左頂點的位置 可設置 print(self.scrollView.contentInset) // scrollView 添加額外的滾動附近區域的內容 可設置 self.scrollView.isDirectionalLockEnabled = true print(self.scrollView.isDirectionalLockEnabled) // 鎖定垂直或水平滾動 可設置 print(self.scrollView.bounces) // scrollView 回彈效果 可設置 print(self.scrollView.alwaysBounceVertical) // 垂直回彈 可設置 print(self.scrollView.alwaysBounceHorizontal) // 水平回彈 可設置 print(self.scrollView.isPagingEnabled) // 分頁 可設置 print(self.scrollView.isScrollEnabled) // 滾動 可設置 print(self.scrollView.showsHorizontalScrollIndicator) // 顯示水平滾動條 print(self.scrollView.showsVerticalScrollIndicator) // 顯示垂直滾動條 print(self.scrollView.scrollIndicatorInsets) // 調整指標insets的內部 print(self.scrollView.indicatorStyle) // black with white border. good against any background print(self.scrollView.decelerationRate) // 減速速度 //open func setContentOffset(_ contentOffset: CGPoint, animated: Bool) 設置 contentOffset //open func scrollRectToVisible(_ rect: CGRect, animated: Bool) 這個方法須要傳入一個Rect 這個rect能夠理解成在scrollview.contentView中frame 調用這個方法就會滾到rect所在的那個區域去 //open func flashScrollIndicators() 短暫地顯示滾動指示器 你應該在把滾動視圖放在最前端時調用此方法。 /* open var isTracking: Bool { get } // returns YES if user has touched. may not yet have started dragging open var isDragging: Bool { get } // returns YES if user has started scrolling. this may require some time and or distance to move to initiate dragging open var isDecelerating: Bool { get } // returns YES if user isn't dragging (touch up) but scroll view is still moving open var delaysContentTouches: Bool // default is YES. if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:. this has no effect on presses open var canCancelContentTouches: Bool // default is YES. if NO, then once we start tracking, we don't try to drag if the touch moves. this has no effect on presses */ //父視圖是否能夠將消息傳遞給子視圖 yes是將事件傳遞給子視圖 則不滾動 no是不傳遞則繼續滾動 //open func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: UIView) -> Bool //父視圖是否能夠將消息傳遞給子視圖 yes是將事件傳遞給子視圖 則不滾動 no是不傳遞則繼續滾動 //open func touchesShouldCancel(in view: UIView) -> Bool print(self.scrollView.minimumZoomScale) // 最小的變焦比例 可設置 print(self.scrollView.maximumZoomScale) // 最大的變焦比例 可設置 print(self.scrollView.zoomScale) // 當前的變焦比例 //open func setZoomScale(_ scale: CGFloat, animated: Bool) 設置變焦比例 //open func zoom(to rect: CGRect, animated: Bool) print(self.scrollView.bouncesZoom) print(self.scrollView.isZooming) print(self.scrollView.isZoomBouncing) print(self.scrollView.scrollsToTop) //當用戶點擊狀態欄時 是否滾動到頂部 //panGestureRecognizer 拖動手勢 //pinchGestureRecognizer 變焦手勢 //keyboardDismissMode 鍵盤消失形式 //refreshControl self.imageView.frame = CGRect(origin:CGPoint(x:0, y:0),size:CGSize(width:width, height:height)) self.scrollView.addSubview(self.imageView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } /// MARK: UIScrollViewDelegate 代理 func scrollViewDidScroll(_ scrollView: UIScrollView) { print("scrollViewDidScroll:scrollView 在滾動") } func scrollViewDidZoom(_ scrollView: UIScrollView) { print("scrollViewDidZoom:scrollView 在改變變焦比例") } func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { print("scrollViewWillBeginDragging:scrollView 即將被拖拽") } func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { print("scrollViewWillEndDragging:scrollView 即將結束拖拽") } func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { print("scrollViewDidEndDragging:scrollView 已經結束拖拽") } func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { print("scrollViewWillBeginDecelerating:scrollView 即將開始減速") } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { print("scrollViewDidEndDecelerating:scrollView 已經開始減速") } func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { print("scrollViewDidEndScrollingAnimation:scrollView 已經結束動畫") } func viewForZooming(in scrollView: UIScrollView) -> UIView? { print("viewForZooming:scrollView 返回視圖(viewForZooming)") return nil } func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { print("scrollViewWillBeginZooming:scrollView 即將開始變焦") } func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) { print("scrollViewDidEndZooming:scrollView 已經結束變焦") } func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { print("scrollViewShouldScrollToTop:scrollView 即將回滾到頂部") return true } func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { print("scrollViewShouldScrollToTop:scrollView 已經回滾到頂部") } }