1. 程式人生 > >swift 4.0 版本 無限輪播圖

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.原始碼地址連結

3.效果圖

這裡寫圖片描述