Swift实现无限轮播效果
从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播。广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们用Swift实现一下。项目地址
图片切换我们可以选择的基本控件有两个UIScrollView和UICollectionView,这次我们选择UICollectionView;既然是轮播,就会用到Timer。所以,我们这次主要应用的知识点为UICollectionView和Timer;
importUIKit classCycleScrollView:UIView,UICollectionViewDelegate,UICollectionViewDataSource{ varbottomView:UICollectionView? varwidth:CGFloat? varheight:CGFloat? vartimer:Timer? overrideinit(frame:CGRect){ super.init(frame:frame) //1.设置背景色 self.backgroundColor=UIColor.clear //2.设置宽高 width=self.frame.size.width height=self.frame.size.height //3.添加bottomView setupBottomView() //4.添加定时器 setupTimer() } requiredinit?(coderaDecoder:NSCoder){ fatalError("init(coder:)hasnotbeenimplemented") } funcsetupBottomView(){ //5.设置collectionView的布局 letflowLayout=UICollectionViewFlowLayout(); flowLayout.itemSize=self.bounds.size flowLayout.minimumLineSpacing=0; flowLayout.minimumInteritemSpacing=0; flowLayout.scrollDirection=UICollectionViewScrollDirection.horizontal; bottomView=UICollectionView.init(frame:self.bounds,collectionViewLayout:flowLayout) self.addSubview(bottomView!); //6.设置collectionView的尺寸 bottomView?.contentSize=CGSize(width:width!*CGFloat(4),height:height!) //7.分页 bottomView?.isPagingEnabled=true //8.去掉滚动条 bottomView?.showsVerticalScrollIndicator=false bottomView?.showsHorizontalScrollIndicator=false //9.设置代理 bottomView?.delegate=self bottomView?.dataSource=self //10.注册cell bottomView?.register(UICollectionViewCell().classForCoder,forCellWithReuseIdentifier:"ID"); if#available(iOS10.0,*){ //11.预加载 bottomView?.isPrefetchingEnabled=true }else{ //Fallbackonearlierversions } } funcsetupTimer(){ //12.实例化定时器 timer=Timer.init(timeInterval:2,target:self,selector:#selector(timerAction),userInfo:nil,repeats:true); RunLoop.main.add(timer!,forMode:RunLoopMode.defaultRunLoopMode); DispatchQueue.main.asyncAfter(deadline:DispatchTime.now()+2){ self.timer?.fire(); } } functimerAction(){ varcontentOffsetX=(self.bottomView?.contentOffset.x)!+self.frame.size.width ifcontentOffsetX>self.frame.size.width*3{ //当前视图显示的是第三个的时候,设置bottomView的偏移量为0 self.bottomView?.contentOffset=CGPoint(x:0,y:0) contentOffsetX=self.frame.size.width } self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0),animated:true) } //重写removeFromSuperview方法,用于删除定时器,否则定时器一直存在,浪费内存 overridefuncremoveFromSuperview(){ timer?.invalidate() timer=nil super.removeFromSuperview() } //Mark:UICollectionViewDataSource //设置Itmes funccollectionView(_collectionView:UICollectionView,numberOfItemsInSectionsection:Int)->Int{ return4; } //设置cell funccollectionView(_collectionView:UICollectionView,cellForItemAtindexPath:IndexPath)->UICollectionViewCell{ letcell:UICollectionViewCell=collectionView.dequeueReusableCell(withReuseIdentifier:"ID",for:indexPath) forview:UIViewincell.contentView.subviews{ view.removeFromSuperview() } letimageView=UIImageView.init(frame:cell.contentView.bounds) ifindexPath.row<3{ imageView.image=UIImage.init(named:String(indexPath.row)) }else{ imageView.image=UIImage.init(named:String(0)) } cell.contentView.addSubview(imageView) returncell; } //Mark:UICollectionViewDelegate //点击方法 funccollectionView(_collectionView:UICollectionView,didSelectItemAtindexPath:IndexPath){ print("您点击了第\(indexPath.row==3?0:indexPath.row)个"); } }
UICollectionView和Timer的用法和OC基本相同。Swift和OC的UI部分应该是一致的,因为底层都是OpenGL。我直接说一下区别:
1.Timer:如果重复,OC是等一个间隔再执行的,Swift是立即执行的,所以我用了GCD延时开启定时器。
2.Swift没有CGPointZero。
无限轮播的原理就是在最后面多添加一个和第一个相同的itme。当你滑动到最后一个itme时,把UICollectionView的contentOffset置零,继续向右活动。如果不添加,会给用户一种卡顿的感觉。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。