Swift實(shí)現(xiàn)無限輪播效果
從今天開始,我學(xué)習(xí)的重點(diǎn)開始轉(zhuǎn)向Swift,并且會分享一些自己學(xué)習(xí)的心得體會,今天給大家?guī)淼牡氖菬o限輪播。廣告頁的無限輪播是非常常見的一個功能,大多數(shù)APP都有,大多數(shù)程序員也都實(shí)現(xiàn)過,今天我們用Swift實(shí)現(xiàn)一下。項(xiàng)目地址
圖片切換我們可以選擇的基本控件有兩個UIScrollView 和 UICollectionView,這次我們選擇UICollectionView;既然是輪播,就會用到Timer。所以,我們這次主要應(yīng)用的知識點(diǎn)為UICollectionView 和 Timer;
import UIKit class CycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource { var bottomView : UICollectionView? var width : CGFloat? var height : CGFloat? var timer : Timer? override init(frame: CGRect){ super.init(frame: frame) // 1.設(shè)置背景色 self.backgroundColor = UIColor.clear // 2.設(shè)置寬高 width = self.frame.size.width height = self.frame.size.height // 3.添加bottomView setupBottomView() // 4.添加定時器 setupTimer() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupBottomView() { // 5.設(shè)置collectionView的布局 let flowLayout = UICollectionViewFlowLayout(); flowLayout.itemSize = self.bounds.size flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal; bottomView = UICollectionView.init(frame: self.bounds, collectionViewLayout: flowLayout) self.addSubview(bottomView!); // 6.設(shè)置collectionView的尺寸 bottomView?.contentSize = CGSize(width:width! * CGFloat(4),height:height!) // 7.分頁 bottomView?.isPagingEnabled = true // 8.去掉滾動條 bottomView?.showsVerticalScrollIndicator = false bottomView?.showsHorizontalScrollIndicator = false // 9.設(shè)置代理 bottomView?.delegate = self bottomView?.dataSource = self // 10.注冊cell bottomView?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier: "ID"); if #available(iOS 10.0, *) { // 11.預(yù)加載 bottomView?.isPrefetchingEnabled = true } else { // Fallback on earlier versions } } func setupTimer() { // 12.實(shí)例化定時器 timer = Timer.init(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true); RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode); DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { self.timer?.fire(); } } func timerAction() { var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width if contentOffsetX > self.frame.size.width * 3 { // 當(dāng)前視圖顯示的是第三個的時候,設(shè)置bottomView的偏移量為0 self.bottomView?.contentOffset = CGPoint(x:0,y:0) contentOffsetX = self.frame.size.width } self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0), animated: true) } // 重寫removeFromSuperview方法,用于刪除定時器,否則定時器一直存在,浪費(fèi)內(nèi)存 override func removeFromSuperview() { timer?.invalidate() timer = nil super.removeFromSuperview() } // Mark:UICollectionViewDataSource // 設(shè)置Itmes func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4; } // 設(shè)置cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ID", for: indexPath) for view : UIView in cell.contentView.subviews { view.removeFromSuperview() } let imageView = UIImageView.init(frame: cell.contentView.bounds) if indexPath.row < 3 { imageView.image = UIImage.init(named: String(indexPath.row)) } else { imageView.image = UIImage.init(named: String(0)) } cell.contentView.addSubview(imageView) return cell; } // Mark:UICollectionViewDelegate // 點(diǎn)擊方法 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("您點(diǎn)擊了第 \(indexPath.row == 3 ? 0 : indexPath.row) 個"); } }
UICollectionView 和 Timer的用法和OC基本相同。Swift和OC的UI部分應(yīng)該是一致的,因?yàn)榈讓佣际荗penGL。我直接說一下區(qū)別:
1.Timer:如果重復(fù),OC是等一個間隔再執(zhí)行的,Swift是立即執(zhí)行的,所以我用了GCD延時開啟定時器。
2.Swift 沒有 CGPointZero。
無限輪播的原理就是在最后面多添加一個和第一個相同的itme。當(dāng)你滑動到最后一個itme時,把UICollectionView的contentOffset置零,繼續(xù)向右活動。如果不添加,會給用戶一種卡頓的感覺。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Swift語言實(shí)現(xiàn)地圖坐標(biāo)彈跳動畫
這篇文章主要介紹了用Swift語言實(shí)現(xiàn)地圖坐標(biāo)彈跳動畫的方法主要應(yīng)用iOS7來實(shí)現(xiàn)此功能,需要的朋友可以參考下2015-07-07詳解Swift的switch...case語句中break關(guān)鍵字的用法
這篇文章主要介紹了Swift的switch...case語句中break關(guān)鍵字的用法,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-04-04Swift、Objective-C、Cocoa混合編程設(shè)置指南
這篇文章主要介紹了Swift、Objective-C、Cocoa混合編程設(shè)置指南,需要的朋友可以參考下2014-07-07Swift編程中的switch...case語句實(shí)例解析
這篇文章主要介紹了Swift編程中的switch...case語句實(shí)例解析,其中重點(diǎn)還是對于fallthrough關(guān)鍵字用法的講解,需要的朋友可以參考下2016-04-04Swift 3.0將UILabel數(shù)字顏色設(shè)置為紅色的方法
這篇文章主要介紹了關(guān)于在Swift中將UILabel數(shù)字顏色設(shè)置為紅色的方法,文中給出了詳細(xì)的示例代碼,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03Swift中內(nèi)置的集合類型學(xué)習(xí)筆記
Swift中自帶數(shù)組、set、字典三大集合類型,這里將學(xué)習(xí)過程中的基礎(chǔ)的Swift中內(nèi)置的集合類型學(xué)習(xí)筆記進(jìn)行整理,需要的朋友可以參考下2016-06-06