您好,登錄后才能下訂單哦!
這篇文章給大家介紹Swift中怎么使用CollectionView實現(xiàn)廣告欄滑動效果,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)建一個類:PlayCollectionViewController.swift
//cell重用標識private let reuseIdentifier = "reuseIdentifier"class PlayCollectionViewController: UICollectionViewController { //頁面?zhèn)€數(shù) private let pageCount = 6 //布局對象(自定義布局) 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() //注冊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的文件中)自定義collectionViewCellclass NewfearureCell: UICollectionViewCell { //保存圖片索引 private var imageIndex:Int? { didSet { //根據(jù)頁碼創(chuàng)建圖片名字(需要每張圖片的名稱只有最有最有一個數(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 { //重寫系統(tǒng)準備布局的方法 override func prepareLayout() { //設置layout布局 itemSize = UIScreen.mainScreen().bounds.size minimumInteritemSpacing = 0 minimumLineSpacing = 0 scrollDirection = UICollectionViewScrollDirection.Horizontal //設置其他屬性 collectionView?.showsHorizontalScrollIndicator = false collectionView?.bounces = false collectionView?.pagingEnabled = true }}
關于Swift中怎么使用CollectionView實現(xiàn)廣告欄滑動效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。