原理:
总共只有三个UIImageView.
1.首先赋值: 显示三个image,把数据源中的第一个Image赋值在中间的UIImageView上,两边一次找到应该赋值的image;
2.更改数据: 滑动后,把数据源中应该显示的image赋值在中间的UIImageView上,然后更改两边的UIImageView上的数据,把scrollView的contentOffset的偏移量设为显示中间图片的偏移量(赋值后肉眼根本无法分辨,但是用户又可以左右滑动了,这就造成了无线循环滑动的假象);
GLCircleView
描述:
swift版的无限循环轮播图,可自定义时间间隔,设置本地、远端图片或混设,目前配合Kingfisher一起使用(url图片赋值ImageView和缓存)。
功能:
运行展示图:
使用方法:
下载后直接把CircleView.swift和CircleView.xib这2个文件拉进项目中
pod添加kingfisher
xib || storyboard:
let imageArray: [GLImageModel] = [GLImageModel("first.jpg",type: .local),GLImageModel("second.jpg",GLImageModel("third.jpg",type: .local)] self.circleView.imageModelArray = imageArray self.circleView.timeInterval = 5 self.circleView.clickCircleViewClosure = { currentIndex in print(currentIndex,terminator: " "); }
code: (能用可视化就用可视化,不要再代码创建了)
let imageArray: [GLImageModel] = [GLImageModel("first.jpg",type: .local)] self.circleView = GLCircleView(frame: CGRect(x: 0,y: 64,width: self.view.frame.size.width,height: 200)) self.circleView.imageModelArray = imageArray self.circleView.timeInterval = 5 self.circleView.clickCircleViewClosure = { currentIndex in print(currentIndex,terminator: " "); } self.view.addSubview(circleView)
添加:
let urlImageModel = GLImageModel(self.circleView.imageModelArray.count % 2 == 0 ? url1 : url2,type: .url) self.circleView.imageModelArray.append(urlImageModel)
下步计划:
如有意见,欢迎issue
项目地址:swift无限自动循环轮播图
原文链接:https://www.f2er.com/swift/326291.html