版本:swift 3.0
思路:将imageArr 前后 添加 imagearr.last 和 imageArr[0] 滑动时能给人循环的假象,设置定时器,调用更换图片方法,通过偏移量,判断是第几张图片,最后设置当手动翻页时调用的方法 scrollViewDidEndDecelerating()在此方法中判断 偏移量 如果是第一张图 更改偏移量到第二个,如果是最后一张图,让偏移量变成第一个,这样就能无限轮播~ 喵~
import UIKit
class HTAdvertisingView: UIView,UIScrollViewDelegate{
typealias Closure = (Any)->Void
let pageControl = UIPageControl()
var timer = Timer()
var currentPage = NSInteger()
var mineImageIndex:NSInteger = 2
//let goBackClosure:Closure!
var imageArr = [UIImage]()
let mineImageView = UIImageView()
let leftImageView = UIImageView()
let rightImageView = UIImageView()
init(images:Array<Any>,frame:CGRect,auto:Bool){
super.init(frame: frame)
self.frame = frame
var substituteArray = images
if auto {
substituteArray.insert(images.last ?? UIImage(), at: 0)
substituteArray.append(images[0])
}
self.imageArr = substituteArray as! [UIImage]
self.configScrollView(isTimer:auto)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//MARK: -代理方法
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.stopTimer()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let page = self.viewWithTag(110)as? UIPageControl
if page != nil{
let width = scrollView.frame.size.width
let point:CGPoint = scrollView.contentOffset
if point.x == 0 {
scrollView.contentOffset = CGPoint.init(x: width * CGFloat(self.imageArr.count - 2), y: 0)
}
if point.x == width * CGFloat(self.imageArr.count-1) {
scrollView.contentOffset = CGPoint.init(x: width , y: 0)
}
currentPage = NSInteger(scrollView.contentOffset.x / width)
page?.currentPage = currentPage - 1
self.startTimer()
}
}
//MARK:-绘制ScrollView
func configScrollView(isTimer:Bool){
let width = self.frame.size.width
let heigth = self.frame.size.height
let scrollView = UIScrollView()
scrollView.frame = self.bounds
scrollView.tag = 100
scrollView.backgroundColor = UIColor.gray
for i in 0..<self.imageArr.count{
let imageView = UIImageView()
imageView.frame = CGRect.init(x:CGFloat(i) * width, y: 0, width: width, height: heigth)
imageView.image = self.imageArr[i]
imageView.backgroundColor = UIColor.red
imageView.isUserInteractionEnabled = true
scrollView.addSubview(imageView)
}
scrollView.contentSize = CGSize.init(width: CGFloat( self.imageArr.count) * width, height: heigth)
let x = isTimer ? width : 0
scrollView.contentOffset = CGPoint.init(x:x, y: 0)
scrollView.bounces = false
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
self.addSubview(scrollView)
if isTimer {
self.startTimer()
self.addPageControl()
}
}
//MARK: -添加PageController
func addPageControl(){
let x = self.frame.size.width/2
let y = self.frame.size.height - 30
self.pageControl.frame = CGRect.init(x: x, y: y, width: x, height: 30)
self.pageControl.numberOfPages = self.imageArr.count - 2
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.white
self.currentPage = 1
self.pageControl.currentPage = self.currentPage-1
self.pageControl.tag = 110
self.addSubview(pageControl)
}
//MARK: -定时器回调方法
func nextImage(){
let scrollView:UIScrollView = self.viewWithTag(100) as! UIScrollView
if currentPage == self.imageArr.count - 2 {
self.currentPage = 1
}else{
self.currentPage += 1
}
let width = scrollView.frame.size.width
scrollView.contentOffset = CGPoint.init(x: CGFloat(currentPage)*width, y: 0)
self.currentPage = NSInteger(scrollView.contentOffset.x)/NSInteger(width)
self.pageControl.currentPage = currentPage - 1
}
//MARK: -定时器开关
func startTimer(){
self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector:#selector(nextImage), userInfo: nil, repeats: true)
}
func stopTimer(){
self.timer.invalidate()
}
//MARK: -作者
/**
* 清凉 -[HT]
*/
}