1.前言
上一篇部落格給大家介紹了Objective-C版的無限輪播封裝
此處給大家獻上Swift版的無限輪播器
幾行代碼搞定圖片輪播器
CollectionView複用cell的機制,不管當前的section有道少了item,當cell的寬和螢幕的寬一致是,當前螢幕最多顯示兩個cell(圖片切換時是兩個cell),切換完成時有且僅有一個cell,即使放大1000倍,記憶體中最多載入兩個cell,所以不會造成記憶體暴漲現象
//輪播圖載入let pointY = 44 + UIApplication.shared.statusBarFrame.size.heightlet 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: CycleViewDelegateextension 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.效果圖