Swift中用CollectionView做广告栏滑动效果

Swift中用CollectionView做广告栏滑动效果,第1张

概述创建一个类:PlayCollectionViewController.swift //cell重用标识private let reuseIdentifier = "reuseIdentifier"class PlayCollectionViewController: UICollectionViewController { //页面个数 private let pageCou 创建一个类:PlayCollectionVIEwController.swift
//cell重用标识private let reuseIDentifIEr = "reuseIDentifIEr"class PlayCollectionVIEwController: UICollectionVIEwController {    //页面个数    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 {            //根据页码创建图片名字(需要每张图片的名称只有最有最有一个数不同)            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 {    //重写系统准备布局的方法    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做广告栏滑动效果全部内容,希望文章能够帮你解决Swift中用CollectionView做广告栏滑动效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1077722.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存