国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift實現無限輪播效果

2020-03-09 17:46:25
字體:
來源:轉載
供稿:網友

從今天開始,我學習的重點開始轉向Swift,并且會分享一些自己學習的心得體會,今天給大家帶來的的是無限輪播。廣告頁的無限輪播是非常常見的一個功能,大多數APP都有,大多數程序員也都實現過,今天我們用Swift實現一下。項目地址

圖片切換我們可以選擇的基本控件有兩個UIScrollView 和 UICollectionView,這次我們選擇UICollectionView;既然是輪播,就會用到Timer。所以,我們這次主要應用的知識點為UICollectionView 和 Timer;

import UIKitclass CycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource {  var bottomView : UICollectionView?  var width : CGFloat?  var height : CGFloat?  var timer : Timer?    override init(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()  }  required init?(coder aDecoder: NSCoder) {    fatalError("init(coder:) has not been implemented")  }      func setupBottomView() {        // 5.設置collectionView的布局    let flowLayout = 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(iOS 10.0, *) {      // 11.預加載      bottomView?.isPrefetchingEnabled = true    } else {      // Fallback on earlier versions    }  }  func setupTimer() {    // 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();    }  }  func timerAction() {        var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width        if contentOffsetX > 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方法,用于刪除定時器,否則定時器一直存在,浪費內存  override func removeFromSuperview() {        timer?.invalidate()    timer = nil    super.removeFromSuperview()  }  // Mark:UICollectionViewDataSource  // 設置Itmes  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        return 4;  }  // 設置cell  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {        let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ID", for: indexPath)    for view : UIView in cell.contentView.subviews {            view.removeFromSuperview()    }    let imageView = UIImageView.init(frame: cell.contentView.bounds)    if indexPath.row < 3 {            imageView.image = UIImage.init(named: String(indexPath.row))    } else {      imageView.image = UIImage.init(named: String(0))    }    cell.contentView.addSubview(imageView)        return cell;  }  // Mark:UICollectionViewDelegate  // 點擊方法  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: 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置零,繼續向右活動。如果不添加,會給用戶一種卡頓的感覺。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金平| 黄冈市| 东乡县| 建德市| 长沙市| 福清市| 五大连池市| 保康县| 五指山市| 石渠县| 辉南县| 华亭县| 潮州市| 麻江县| 平乐县| 隆回县| 临沂市| 屏南县| 洮南市| 论坛| 确山县| 黑山县| 英超| 萍乡市| 乐昌市| 图木舒克市| 姚安县| 阜城县| 光泽县| 云南省| 黎平县| 分宜县| 咸宁市| 彭阳县| 潼关县| 中牟县| 新营市| 东港市| 家居| 新闻| 西丰县|