任何一门语言,只要长期不用就会忘掉,得时不时的敲敲小项目,练练手;
let scrollViewBG = UIScrollView.init(frame: SLScreenRect)
let images = ["first","second","third"]
let pageControl = UIPageControl.init(frame: CGRect.init(x: 0, y: SLScreenHeight - 30, width: SLScreenWidth, height: 20))
var currentPage = 0
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureView()
}
func configureView() {
// 遍历数组,同时获得index
for (index, value) in images.enumerated() {
let imageView = UIImageView.init(frame: CGRect.init(x: CGFloat(index)*SLScreenWidth, y: 0, width: SLScreenWidth, height: SLScreenHeight))
imageView.image = UIImage.init(named: value)
// 限制边界
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
scrollViewBG .addSubview(imageView)
}
// 协议
scrollViewBG.delegate = self
scrollViewBG.isPagingEnabled = true
scrollViewBG.showsHorizontalScrollIndicator = false
scrollViewBG.contentSize = CGSize.init(width: SLScreenWidth*CGFloat(images.count), height: SLScreenHeight)
// pagecontol
pageControl.numberOfPages = images.count
pageControl.currentPageIndicatorTintColor = .white
pageControl.pageIndicatorTintColor = .lightGray
pageControl.currentPage = currentPage
pageControl.isEnabled = false
view.addSubview(scrollViewBG)
view.addSubview(pageControl)
}
//MARK: - 协议方法
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
currentPage = Int(scrollView.contentOffset.x/SLScreenWidth)
pageControl.currentPage = currentPage
}