swift 4.0 版本 无限轮播图

1.前言

上一篇博客给大家介绍了Objective-C版的无限轮播封装
此处给大家献上Swift版的无限轮播器

几行代码搞定图片轮播器
CollectionView复用cell的机制,不管当前的section有道少了item,当cell的宽和屏幕的宽一致是,当前屏幕最多显示两个cell(图片切换时是两个cell),切换完成时有且仅有一个cell,即使放大1000倍,内存中最多加载两个cell,所以不会造成内存暴涨现象

//轮播图加载
let pointY = 44 + UIApplication.shared.statusBarFrame.size.height
let cycleView : CycleView = CycleView(frame: CGRect(x: 0, y: pointY, width: UIScreen.main.bounds.size.width, height: 220))

cycleView.mode = .scaleAspectFill
//本地图片测试--加载网络图片,请用第三方库如SDWebImage等
cycleView.imageURLStringArr = ["banner01.jpg", "banner02.jpg", "banner03.jpg", "banner04.jpg"]
tableView.tableHeaderView = cycleView

增加了代理方法

//MARK: CycleViewDelegate
extension HomeController {
    func cycleViewDidSelectedItemAtIndex(_ index: NSInteger) {
        let demoVc = DemoController()
        demoVc.title = "点击了轮播图第\(index)个图片"
        demoVc.view.backgroundColor = UIColor.white
        navigationController?.pushViewController(demoVc, animated: true)
    }
}

2.源码地址链接

ZYCycleViewSwift版 GitHub链接

3.效果图

你可能感兴趣的:(Swift,2.0~4.2,iOS开发)