Swift使用CollectionView实现广告栏滑动效果
2019-06-22 15:29
1386 查看
本文实例为大家分享了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 } }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Swift - 使用CollectionView实现图片Gallery画廊效果(左右滑动浏览图片)
- 使用axure实现的滑动效果
- Android中使用ViewPager和PagerTabStrip实现页面滑动效果
- 使用ViewPager实现图片自动播放效果,并可左右滑动
- Qt使用QGraphicsView实现滑动窗体效果
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- Android使用ViewPager实现左右循环滑动及轮播效果
- Android使用ViewPager实现左右滑动效果
- 使用ViewPager+GridView实现横向滑动的效果(一)
- 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
- Android使用ViewPager实现左右循环滑动及轮播效果
- 使用ViewPager实现页面滑动(点击)跳转效果(类似于微信页面)
- iphone手机桌面滑动效果使用css3实现
- android-使用ViewGroup实现左右滑动切换界面以及界面裂开的效果
- android viewflipper的使用 实现图片滑动效果
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果