scrollView的偏移和縮放、pageControl(小點)和scrollView的連線
cg```swift
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var redView: UIView!
var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
//建立scrollView
scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 200))
scrollView.backgroundColor = UIColor.cyanColor()
self.view.addSubview(scrollView)
//在scrollview上新增兩個標記View
redView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
redView.backgroundColor = UIColor.redColor()
scrollView.addSubview(redView)
let greenView = UIView(frame: CGRect(x: scrollView.frame.size.width - 50, y: scrollView.frame.size.height - 50, width: 50, height: 50))
greenView.backgroundColor = UIColor.redColor()
scrollView.addSubview(greenView)
//UIScrollView能否滾動
//1. scrollEnabled
//2. contentSize: (0, 0) 內容大小,只有當內容尺寸超過本身尺寸時
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * 3, height: scrollView.frame.size.height)
//設定內容距四周的距離(邊框大小)
scrollView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0)
//設定偏移量(向左、向上滾動為正)
scrollView.contentOffset = CGPoint(x: 100, y: 0)
//方向鎖, 一次只允許一個方向滾動
scrollView.directionalLockEnabled = true
//彈性效果
scrollView.bounces = false
//水平滾動條
scrollView.showsVerticalScrollIndicator = false
//垂直滾動條
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
//最小縮放倍數
scrollView.minimumZoomScale = 1
//最大縮放倍數
scrollView.maximumZoomScale = 5
//小點處於控制元件中央
pageControl = UIPageControl(frame: CGRect(x: 0, y: scrollView.frame.origin.y + scrollView.frame.size.height - 20, width: scrollView.frame.size.width, height: 20))
//設定小點個數
pageControl.numberOfPages = 3
//設定當前小點顏色
pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
//設定其餘小點顏色
pageControl.pageIndicatorTintColor = UIColor.yellowColor()
self.view.addSubview(pageControl)
//給小點新增點選事件,將點選小點的事件連線到scrollView上
pageControl.addTarget(self, action: #selector(didChange(_:)), forControlEvents: .ValueChanged)
}
func didChange(sender: UIPageControl){
let offset = CGPoint(x: scrollView.bounds.size.width * CGFloat(sender.currentPage), y: 0)
//1. 沒有動畫效果
// scrollView.contentOffset = offset
//2. 有動畫效果
// scrollView.setContentOffset(offset, animated: true)
//3. 將一個矩形區域設為可見
let rect = CGRect(x: offset.x, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)
scrollView.scrollRectToVisible(rect, animated: true)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
//只要有位移就會持續呼叫
print(scrollView.contentOffset)
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
print("begin dragging")
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("結束拖動")
}
// 一般就只用下面兩個函式
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate {
print("開始減速")
}
else {
print("無需減速")
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
print("停止減速")
//把scrollView的偏移情況在小點上面顯示
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
//設定scrollview上要縮放的View, 返回值為View
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return redView
}
}
相關文章
- ScrollViewView
- Android UI系列-----ScrollView和HorizontalScrollViewAndroidUIView
- IOS ScrollViewiOSView
- Android Webview和ScrollView衝突和WebView使用總結AndroidWebView
- Masonry 關於ScrollView的使用View
- React-Native學習系列(二) Image和ScrollViewReactView
- NGUI學習筆記(六):ScrollView、Grid和TableNGUI筆記View
- scrollview 套gridview的問題View
- ScrollView中用ListView的事件衝突View事件
- 理解 TCP(三):連線的建立和釋放TCP
- ScrollView 觸控事件View事件
- android listview and scrollviewAndroidView
- Android之重寫ScrollView實現兩個ScrollView的同步滾動顯示AndroidView
- iOS開發-探索scrollView的實現iOSView
- iOS和Android的點對點連線iOSAndroid
- Android帶有粘性頭部的ScrollViewAndroidView
- Xamarin.Forms的滾動檢視ScrollViewORMView
- 【網路協議】TCP連線的建立和釋放協議TCP
- Scrollview 和 內部 recycleview 高度固定時巢狀衝突的一種解決方法View巢狀
- Android ScrollView滾動到指定View的位置AndroidView
- ScrollView中巢狀GridView,Listview的辦法View巢狀
- android 自定義ScrollView實現背景圖片伸縮的實現程式碼及思路AndroidView
- ScrollView上新增pageView並加CollectionViewView
- React Native 中 ScrollView 效能探究React NativeView
- scrollview滾動到指定位置View
- 長連線和短連線的使用
- http的長連線和短連線HTTP
- Oracle的左連線和右連線Oracle
- Kerberos和NTLM - SQL Server連線的那點事ROSSQLServer
- TCP連結的建立和釋放TCP
- scrollview 的滑動衝突 viewpager等都適用Viewpager
- 【程式設計框架】Android可配置的ScrollView框架程式設計框架AndroidView
- 實現一個帶下拉彈簧動畫的 ScrollView動畫View
- ScrollView 之 實現檢視的迴圈顯示View
- AutoLayout 實現固定寬度動態高度的 ScrollViewView
- 精確計算微信小程式scrollview高度,全機型適配微信小程式View
- 等值連線和自然連線的區別
- SQL中的左連線和右連線SQL