Swift使用CollectionView实现广告栏滑动效果
更新时间:2019年06月22日 15:29:37 作者:Stevin的技术博客
这篇文章主要为大家详细介绍了Swift使用CollectionView实现广告栏滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Swift实现广告栏滑动效果的具体代码,供大家参考,具体内容如下
创建一个类: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的文件中)自定义collectionViewCell class 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 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
iOS中Swift指触即开集成Touch ID指纹识别功能的方法
随着移动支付时代的到来,Touch ID 指纹验证迅速被支付宝,微信钱包普及,相信各位朋友使用后也大呼方便。下面给大家分享iOS中Swift指触即开集成Touch ID指纹识别功能的方法,一起看看吧2017-03-03简陋的swift carthage copy-frameworks 辅助脚本代码
下面小编就为大家分享一篇简陋的swift carthage copy-frameworks 辅助脚本代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-01-01
最新评论