Swift中怎么使用CollectionView实现广告栏滑动效果

这篇文章给大家介绍Swift中怎么使用CollectionView实现广告栏滑动效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名、网络空间、营销软件、网站建设、巴东网站维护、网站推广。

创建一个类: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://www.cdcxhl.com/article32/peigpc.html

成都网站建设公司_创新互联,为您提供企业建站微信公众号动态网站网站设计网站导航虚拟主机

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站