Swift collectionView 无限轮播

Swift collectionView 无限轮播,第1张

概述  一:封装成CycleView import UIKit//无线轮播代理protocol CycleViewDelegate:class { func CycleViewItemClick(_ collectionView:UICollectionView,selectedItem item:Int)}//无限轮播的封装class CycleView: UIView,UICo

 

一:封装成CycleVIEw
import UIKit//无线轮播代理protocol CycleVIEwDelegate:class {    func CycleVIEwItemClick(_ collectionVIEw:UICollectionVIEw,selectedItem item:Int)}//无限轮播的封装class CycleVIEw: UIVIEw,UICollectionVIEwDelegate,UICollectionVIEwDataSource {        var collectionVIEw:UICollectionVIEw!    var wIDth:CGfloat!    var height:CGfloat!    var imagenames:[String]!    var timer:Timer?    var startContentOffsetX:CGfloat = 0    var item:Int = 0    var pageControl : UIPageControl?    weak var delegate:CycleVIEwDelegate?    var timeInterval:Double?    /// frame:collectionVIEw 的frame    /// iamgenames:图片名    /// timeInterval:自动滚动的时间间隔    /// pageControl:默认设置居中        init(frame: CGRect,imagenames:[String],timeInterval:Double=2,pageControl:UIPageControl?=nil) {        super.init(frame: frame)        self.imagenames = imagenames        self.pageControl = pageControl        self.timeInterval = timeInterval        self.wIDth = frame.wIDth        self.height = frame.height        setupCollectionVIEw()        setupTimer()    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    //设置定时器    func setupTimer() {        timer = Timer.scheduledTimer(timeInterval: timeInterval!,target: self,selector: #selector(nextPage),userInfo: nil,repeats: true)        RunLoop.main.add(timer!,forMode: .commonModes)    }    //自动播放下一页    @objc func nextPage(){        //获取当前indexpath        let currentIndexPath = collectionVIEw.indexPathsForVisibleItems.last        //滚动到中间的section        let mIDdleIndexPath = IndexPath(item: (currentIndexPath?.item)!,section: 1)        //滚动到中间section        collectionVIEw.scrollToItem(at: mIDdleIndexPath,at: .left,animated: false)        //滚动到目标页面        var nextItem = mIDdleIndexPath.item + 1        var nextSection = mIDdleIndexPath.section        if nextItem == imagenames.count {            nextItem = 0            nextSection += 1        }        collectionVIEw.scrollToItem(at: IndexPath(item: nextItem,section: nextSection),animated: true)    }    //设置collectionVIEw    func  setupCollectionVIEw(){        let flowLayout = UICollectionVIEwFlowLayout()        flowLayout.scrollDirection = .horizontal        flowLayout.minimumlinespacing = 0        flowLayout.minimumInteritemSpacing = 0        flowLayout.itemSize = self.bounds.size        collectionVIEw = UICollectionVIEw(frame: self.bounds,collectionVIEwLayout: flowLayout)        collectionVIEw.isPagingEnabled = true        collectionVIEw.dataSource = self        collectionVIEw.delegate  = self        collectionVIEw.contentSize = CGSize(wIDth: wIDth*CGfloat(imagenames.count),height: height)        collectionVIEw.showsverticalScrollindicator = false        collectionVIEw.showsHorizontalScrollindicator = false        collectionVIEw.register(UICollectionVIEwCell.self,forCellWithReuseIDentifIEr: "IDentify")        self.addSubvIEw(collectionVIEw)        //设置pageControl        if pageControl == nil{            setupPageControl()        }else{            addSubvIEw(pageControl!)        }        collectionVIEw.scrollToItem(at: IndexPath(item: 0,section: 1),animated: false)    }    //设置pageControl    func setupPageControl(){        let rect = CGRect(x: Int(wIDth/2-50),y: Int(height-30),wIDth: 100,height: 20)        pageControl = UIPageControl(frame: rect)        pageControl?.numberOfPages = imagenames.count        pageControl?.currentPageIndicatorTintcolor = UIcolor.red        pageControl?.isUserInteractionEnabled = false        addSubvIEw(pageControl!)    }    //重置定时器    func resetTimer() {        timer?.invalIDate()        timer = nil            }    // 默认三个section    func numberOfSections(in collectionVIEw: UICollectionVIEw) -> Int {        return 3    }    //每个section的个数    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {        return imagenames.count    }    //返回cell    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell {        let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: "IDentify",for: indexPath)        for vIEw:UIVIEw in cell.contentVIEw.subvIEws{            vIEw.removeFromSupervIEw()        }        let imageVIEw = UIImageVIEw(frame: cell.contentVIEw.bounds)        imageVIEw.image = UIImage(named: imagenames[indexPath.row])        cell.contentVIEw.addSubvIEw(imageVIEw)        return cell    }    //选中item处理    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,dIDSelectItemAt indexPath: IndexPath) {        delegate?.CycleVIEwItemClick(collectionVIEw,selectedItem: indexPath.item)    }    //完成滚动时,设置pageControl    func scrollVIEwDIDScroll(_ scrollVIEw: UIScrollVIEw) {        let page = Int((scrollVIEw.contentOffset.x+wIDth*0.5)/wIDth)        let currentPage = page%imagenames.count        pageControl?.currentPage = currentPage    }    //开始拖动,移除定时器    func scrollVIEwWillBeginDragging(_ scrollVIEw: UIScrollVIEw) {        resetTimer()    }    //完成拖动,重新添加定时器    func scrollVIEwDIDEndDragging(_ scrollVIEw: UIScrollVIEw,willDecelerate decelerate: Bool) {        self.setupTimer()    }    //手动滑动处理    func scrollVIEwDIDEndDecelerating(_ scrollVIEw: UIScrollVIEw) {        collectionVIEw.scrollToItem(at: IndexPath(item: (pageControl?.currentPage)!,animated: false)    }}
二:调用
import UIKit//遵守代理class VIEwController: UIVIEwController,CycleVIEwDelegate {    //实现代理方法    func CycleVIEwItemClick(_ collectionVIEw: UICollectionVIEw,selectedItem item: Int) {        print(item)    }    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        //图片名        let imageArr = ["FirstPicture","SecondPicture","ThirdPicture"]        //滚动视图的frame        let rect = CGRect(x: 0,y: 20,wIDth: UIScreen.main.bounds.wIDth,height: UIScreen.main.bounds.wIDth*9/16)                //1.默认pageControl居中,默认timeInterval为2s,        let cycleVIEw =  CycleVIEw(frame: rect,imagenames: imageArr,timeInterval:3)        //实现代理        cycleVIEw.delegate = self        //添加到VIEw        self.vIEw.addSubvIEw(cycleVIEw)                //       2.自定义pageControl的位置和属性        //        let pageControl = UIPageControl(frame: CGRect(x: 100,y: 100,wIDth: 100,height: 40))        //        pageControl.currentPageIndicatorTintcolor = UIcolor.blue        //        pageControl.isUserInteractionEnabled = false        //        pageControl.numberOfPages = imageArr.count        //        let cycleVIEw = CycleVIEw(frame: rect,pageControl: pageControl)        //        cycleVIEw.delegate = self        //        self.vIEw.addSubvIEw(cycleVIEw)    }}
三:效果

总结

以上是内存溢出为你收集整理的Swift collectionView 无限轮播全部内容,希望文章能够帮你解决Swift collectionView 无限轮播所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1024296.html

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

发表评论

登录后才能评论

评论列表(0条)

保存