SWIFT|SWIFT 实现banner无限轮播
实现思路
【SWIFT|SWIFT 实现banner无限轮播】代码实现
class AppBannerView: UIScrollView,UIScrollViewDelegate{var pageChangeDelegate:PageChangeDelegate?
//记录当前的page
var currentPage = 0
var photos:[String]?
var autoScroll = true
var nsTimer:NSTimer?init(frame:CGRect,photos:[String]!){
self.photos = photos
super.init(frame: frame)
initView()
}required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}override func awakeFromNib() {
initView()
}func initView(){self.delegate = self
self.pagingEnabled = true
self.showsHorizontalScrollIndicator = false
self.showsVerticalScrollIndicator = true
for (var i = 0;
i<3;
i++){
let view = UIImageView(frame: CGRectMake(self.bounds.width*CGFloat(i), 0, self.bounds.width, self.bounds.height))
view.contentMode = UIViewContentMode.ScaleToFillvar imageUrl:NSURL?
if i == 0{
imageUrl = NSURL(string: photos![photos!.count-1])
}else if i == 2{
imageUrl = NSURL(string: photos![1])
}else{
imageUrl = NSURL(string: photos![0])
}view.sd_setImageWithURL(imageUrl)
self.addSubview(view)}self.contentSize = CGSizeMake(self.bounds.width*CGFloat(photos!.count), 0)
self.setContentOffset(CGPoint(x: self.bounds.width, y: 0), animated: false)if autoScroll{nsTimer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("startScroll"), userInfo: nil, repeats: true)
}
}func showPageIndex(index:Int){if index > currentPage{self.setContentOffset(CGPointMake(self.bounds.width*2, 0), animated: true)
}else{
self.setContentOffset(CGPointMake(0, 0), animated: true)
}
}func setDataSource(photos:[String]!){self.photos = photos
}func scrollViewDidScroll(scrollView: UIScrollView) {
if abs((scrollView.contentOffset.x - self.bounds.width)) >= self.bounds.width{if scrollView.contentOffset.x == 0{
currentPage= currentPage - 1
if currentPage == -1 {currentPage = photos!.count-1
}
}else{currentPage =currentPage + 1if currentPage == photos!.count{currentPage = 0
}}
pageChangeDelegate?.pageChange(currentPage)self.setContentOffset(CGPoint(x: self.bounds.width, y: 0), animated: false)
let imageView =self.subviews[1] as! UIImageView
imageView.sd_setImageWithURL(NSURL(string: photos![currentPage])!)
let leftImageView = self.subviews[0] as! UIImageView
if currentPage-1 == -1{leftImageView.sd_setImageWithURL(NSURL(string: photos![photos!.count-1]))
}else{
leftImageView.sd_setImageWithURL(NSURL(string: photos![currentPage-1]))
}
let rightImageView = self.subviews[2] as! UIImageView
if currentPage+1 == photos!.count {
rightImageView.sd_setImageWithURL(NSURL(string: photos![0]))
}else{
rightImageView.sd_setImageWithURL(NSURL(string: photos![currentPage+1]))
}
}}func startScroll(){self.showPageIndex(currentPage+1)
}func stopScroll(){nsTimer?.invalidate()
}//默认滚动位置 屏幕的宽度按func scrollViewDidEndDecelerating(scrollView: UIScrollView) {}func setBannerViewAutoScroll(autoScroll:Bool){self.autoScroll = autoScrollif !autoScroll{stopScroll()
}}}protocol PageChangeDelegate{func pageChange(index:Int)
}
推荐阅读
- 小程序|微信小程序云开发实现聊天(聊天室,一对一聊天)
- Java|Java JDK 实现 DES1 加解密
- JavaScript获取时区实现过程解析
- axios|axios 拦截器管理类链式调用手写实现及原理剖析
- 左邻云资管|助力园区实现资产360°管控
- clickhouse|clickhouse join_HDFS+ClickHouse+Spark(从0到1实现一款轻量级大数据分析系统)
- 后端|【python】Jenkins实现携带commit_log钉钉/企微机器人通知
- {调取该文章的TAG关键词}|中国人寿寿险发半年报,实现总保费4399.69亿元 | 品牌
- SQL|SQL Server实现group_concat功能的详细实例
- springboot+mybatis拦截器方法实现水平分表操作