Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果
本文實(shí)例為大家分享了Swift實(shí)現(xiàn)廣告欄滑動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)建一個(gè)類:PlayCollectionViewController.swift
//cell重用標(biāo)識(shí) private let reuseIdentifier = "reuseIdentifier" class PlayCollectionViewController: UICollectionViewController { //頁(yè)面?zhèn)€數(shù) private let pageCount = 6 //布局對(duì)象(自定義布局) private var layout: UICollectionViewFlowLayout = PlayLayout() init() { super.init(collectionViewLayout: layout) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() //注冊(cè)cell collectionView?.registerClass(NewfearureCell.self, forCellWithReuseIdentifier: reuseIdentifier) } //MARK: - UICollectionDataSource override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return pageCount } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfearureCell cell.imageIndex = indexPath.item return cell } }
自定義CollectionView的cell
//MARK: - 在這里我們依然在(PlayCollectionViewController.swift的文件中)自定義collectionViewCell class NewfearureCell: UICollectionViewCell { //保存圖片索引 private var imageIndex:Int? { didSet { //根據(jù)頁(yè)碼創(chuàng)建圖片名字(需要每張圖片的名稱只有最有最有一個(gè)數(shù)不同) iconView.image = UIImage(named: "image_\(imageIndex!)") } } override init(frame: CGRect) { super.init(frame: frame) //初始化UI setupUI() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupUI() { //添加子控件到contentView contentView.addSubview(iconView) //布局子控件位置(填充屏幕) iconView.xmg_Fill(contentView) } //MARK: 懶加載 private lazy var iconView = UIImageView() }
繼承UICollectionViewFlowLayout,自定義布局
private class PlayLayout: UICollectionViewFlowLayout { //重寫(xiě)系統(tǒng)準(zhǔn)備布局的方法 override func prepareLayout() { //設(shè)置layout布局 itemSize = UIScreen.mainScreen().bounds.size minimumInteritemSpacing = 0 minimumLineSpacing = 0 scrollDirection = UICollectionViewScrollDirection.Horizontal //設(shè)置其他屬性 collectionView?.showsHorizontalScrollIndicator = false collectionView?.bounces = false collectionView?.pagingEnabled = true } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Swift解決UITableView空數(shù)據(jù)視圖問(wèn)題的簡(jiǎn)單方法
欄 目:Swift
下一篇:Swift中優(yōu)雅處理閉包導(dǎo)致的循環(huán)引用詳解
本文標(biāo)題:Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果
本文地址:http://mengdiqiu.com.cn/a1/Swift/11924.html
您可能感興趣的文章
- 01-11swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景詳解
- 01-11Swift利用Decodable解析JSON的一個(gè)小問(wèn)題詳解
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中定義單例的方法實(shí)例
- 01-11Swift利用純代碼實(shí)現(xiàn)時(shí)鐘效果實(shí)例代碼
- 01-11Swift中排序算法的簡(jiǎn)單取舍詳解
- 01-11Swift如何為設(shè)置中心添加常用功能
- 01-11Swift Json實(shí)例詳細(xì)解析
- 01-11Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11Swift利用Decodable解析JSON的一個(gè)小問(wèn)題
- 01-11swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景詳解
- 01-11Swift中初始化init的方法小結(jié)
- 01-11Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
- 01-11Swift利用純代碼實(shí)現(xiàn)時(shí)鐘效果實(shí)例代碼
- 01-11Swift中定義單例的方法實(shí)例
- 01-11Swift中排序算法的簡(jiǎn)單取舍詳解
- 01-11Swift Json實(shí)例詳細(xì)解析
- 01-11Swift如何為設(shè)置中心添加常用功能
- 01-11Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)