import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
let MainWidth = UIScreen.main.bounds.width
var ascrollViews = UIScrollView()
var pageControl:UIPageControl?
override func viewDidLoad() {
super.viewDidLoad()
//基础知识
self.createScrollViews()
}
//基础知识
func createScrollViews() {
let scrollvc = UIScrollView(frame: CGRect(x: 0, y: 64, width: MainWidth, height: 300))
self.view.addSubview(scrollvc)
self.ascrollViews = scrollvc
scrollvc.backgroundColor = UIColor.gray
//显示水平指示条
scrollvc.showsHorizontalScrollIndicator = true
//调整指示器(滚动条)的位置
// scrollvc.scrollIndicatorInsets = UIEdgeInsetsMake(20, 50, 50, 50)
//设置指示器(滚动条)的样式
scrollvc.indicatorStyle = UIScrollViewIndicatorStyle.black
scrollvc.delegate = self
//但是可以设定,内容就算小于它,也能拖:拖着任意方向
// scrollvc.alwaysBounceVertical = true //还有水平的
//显示垂直指示条
scrollvc.showsVerticalScrollIndicator = false
//设置是否可以拉出空白区域、回弹
// scrollvc.bounces = true
//设置分页滚动
scrollvc.isPagingEnabled = true
//默认是false。如果是true并且bounces也是true,即使内容尺寸比scrollView的尺寸小,也能水平推动
scrollvc.alwaysBounceHorizontal = true
//允许滑动视图本身,如果设为false就不能触发拖动代理事件
scrollvc.isScrollEnabled = true
//在scrollView的内容周围添加一个附件的区域
// scrollvc.contentInset = UIEdgeInsetsMake(120, 30, 30, 80)
//设置内容区域大小
scrollvc.contentSize = CGSize(width: MainWidth*3, height: 300)
let imageView = UIImageView.init(frame: CGRect(x: 0, y: 10, width: MainWidth, height: 290))
scrollvc.addSubview(imageView)
imageView.image = #imageLiteral(resourceName: "image12")
let tap1 = UITapGestureRecognizer(target: self, action: #selector(modelAction))
imageView.addGestureRecognizer(tap1)
let imageView2 = UIImageView.init(frame: CGRect(x: MainWidth, y: 10, width: MainWidth, height: 290))
scrollvc.addSubview(imageView2)
imageView2.image = #imageLiteral(resourceName: "image13")
imageView2.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(modelAction))
imageView2.addGestureRecognizer(tap)
let imageView23 = UIImageView.init(frame: CGRect(x: MainWidth*2, y: 10, width: MainWidth, height: 290))
scrollvc.addSubview(imageView23)
imageView23.image = #imageLiteral(resourceName: "image15")
// 缩小的最小比例
// scrollvc.minimumZoomScale = 0.3
// 放大的最大比例
// scrollvc.maximumZoomScale = 5.6
pageControl = UIPageControl(frame: CGRect(x: 10, y: 334, width: MainWidth-20, height: 30))
pageControl?.backgroundColor = UIColor.red
//设置显示的页数
pageControl?.numberOfPages = 3
//设置显示的起始页的索引
pageControl?.currentPage = 0
//设置单页时隐藏
pageControl?.hidesForSinglePage = true
//设置显示颜色
pageControl?.currentPageIndicatorTintColor = UIColor.green
//设置页背景指示颜色
pageControl?.pageIndicatorTintColor = UIColor.lightGray
//添加事件
pageControl?.addTarget(self, action: #selector(pageControlChanged), for: .valueChanged)
self.view.addSubview(pageControl!)
}
// 1、已经开始滚动(不管是拖、拉、放大、缩小等导致)都会执行此函数
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("已经开始滚动(不管是拖、拉、放大、缩小等导致)都会执行此函数")
let offset = scrollView.contentOffset.x / MainWidth
pageControl?.currentPage = Int(offset)
}
// 2、将要开始拖拽,手指已经放在view上并准备拖动的那一刻
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("将要开始拖拽,手指已经放在view上并准备拖动的那一刻")
}
// 3、将要结束拖拽,手指已拖动过view并准备离开手指的那一刻
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("将要结束拖拽,手指已拖动过view并准备离开手指的那一刻-----")
}
// 4、已经结束拖拽,手指刚离开view的那一刻
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("----------------已经结束拖拽,手指刚离开view的那一刻")
}
func modelAction() {
let other = OtherViewController()
self.present(other, animated: true) {
}
}
//pageControl的触发事件
func pageControlChanged(pageControl:UIPageControl) {
let page = pageControl.currentPage
print("当前显示的是第\(page)页")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}