zoukankan      html  css  js  c++  java
  • swift中使用UIColllectionView实现横向轮播的一般方法

    //

    //  HomeLiveRankCell.swift

    //  YYSwiftProject

    //

    //  Created by Domo on 2018/7/28.

    //  Copyright © 2018年 知言网络. All rights reserved.

    //

     

    import UIKit

    class HomeLiveRankCell: UICollectionViewCell {

        

        private var multidimensionalRankVosList: [MultidimensionalRankVosModel]?

        

        private let LiveRankCellID = "LiveRankCell"

        // MARK: - 滚动排行榜

        private lazy var collectionView: UICollectionView = {

            let layout = UICollectionViewFlowLayout.init()

            layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)

            layout.minimumInteritemSpacing = 0

            layout.minimumLineSpacing = 0

            layout.itemSize = CGSize( (YYScreenWidth-30), height:self.frame.size.height)

    //        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)

            layout.scrollDirection = UICollectionViewScrollDirection.horizontal

            let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)

            collectionView.contentSize = CGSize( (YYScreenWidth-30), height: self.frame.size.height)

            collectionView.delegate = self

            collectionView.dataSource = self

            collectionView.backgroundColor = UIColor.white

            collectionView.showsVerticalScrollIndicator = false

            collectionView.showsHorizontalScrollIndicator = false

            collectionView.isPagingEnabled = true

            collectionView.register(LiveRankCell.self, forCellWithReuseIdentifier:LiveRankCellID)

            

            return collectionView

        }()

        

        var timer: Timer?

        override init(frame: CGRect) {

            super.init(frame: frame)

            self.backgroundColor = UIColor.white

            self.addSubview(self.collectionView)

            self.collectionView.snp.makeConstraints { (make) in

                make.width.height.equalToSuperview()

                make.center.equalToSuperview()

            }

            

            // 开启定时器

            starTimer()

        }

        

        required init?(coder aDecoder: NSCoder) {

            super.init(coder: aDecoder)

        }

        

        // 界面赋值并刷新

        var multidimensionalRankVos:[MultidimensionalRankVosModel]? {

            didSet {

                guard let model = multidimensionalRankVos else { return }

                self.multidimensionalRankVosList = model

                self.collectionView.reloadData()

            }

        }

    }

    extension HomeLiveRankCell: UICollectionViewDataSource, UICollectionViewDelegate {

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            return (self.multidimensionalRankVosList?.count ?? 0)*100

        }

        

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell:LiveRankCell = collectionView.dequeueReusableCell(withReuseIdentifier: LiveRankCellID, for: indexPath) as! LiveRankCell

            cell.backgroundColor = UIColor.init(red: 248/255.0, green: 245/255.0, blue: 246/255.0, alpha: 1)

            cell.multidimensionalRankVos = self.multidimensionalRankVosList?[indexPath.row%(self.multidimensionalRankVosList?.count)!]

            return cell

        }

        

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

            print(indexPath.row%(self.multidimensionalRankVosList?.count)!)

        }

        

        /// 开启定时器

        func starTimer () {

            let timer = Timer.init(timeInterval: 3, target: self, selector: #selector(nextPage), userInfo: nil, repeats: true)

            // 这一句代码涉及到runloop 和 主线程的知识,则在界面上不能执行其他的UI操作

            RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

            self.timer = timer

        }

        

        /// 在1秒后,自动跳转到下一页

        @objc func nextPage() {

            // 1.获取collectionView的X轴滚动的偏移量

            let currentOffsetX = collectionView.contentOffset.x

            let offsetX = currentOffsetX + collectionView.bounds.width

            // 2.滚动该位置

            collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)

        }

        

    //    // 监听collectionView的滚到

    //    func scrollViewDidScroll(_ scrollView: UIScrollView) {

    //        // 1、获取滚动的偏移量 + scrollView.bounds.width * 0.5给偏移量加一半,当滑动一般就滚动pageControl的当前选中

    //        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5

    //        // 2、计算pageContra的currentIndex。这 % (cycleModelArr?.count ?? 1)也是跟上同样道理

    //        pageControl.currentPage = Int(offsetX / scrollView.bounds.width) % (imageArray?.count ?? 1)

    //    }

    //

        /// 当collectionView开始拖动的时候,取消定时器

        func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

            self.timer?.invalidate()

            self.timer = nil

        }

        

        /// 当用户停止拖动的时候,开启定时器

        func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

            starTimer()

        }

    }

    //

    //  LiveRankCell.swift

    //  YYSwiftProject

    //

    //  Created by Domo on 2018/7/28.

    //  Copyright © 2018年 知言网络. All rights reserved.

    //

    import UIKit

    class LiveRankCell: UICollectionViewCell {

        private lazy var imageView : UIView = {

            let imageView = UIView()

            return imageView

        }()

        

        private lazy var titleLabel : UILabel = {

           let label = UILabel()

            label.font = UIFont.systemFont(ofSize: 18)

            return label

        }()

        

        private lazy var label : UILabel = {

            let label = UILabel()

            label.text = ">"

            label.textColor = UIColor.lightGray

            return label

        }()

        

        override init(frame: CGRect) {

            super.init(frame: frame)

            

            self.addSubview(self.titleLabel)

            self.titleLabel.snp.makeConstraints { (make) in

                make.left.equalToSuperview().offset(10)

                make.height.equalTo(40)

                make.width.equalTo(100)

                make.centerY.equalToSuperview()

            }

            

            self.addSubview(self.imageView)

            self.imageView.snp.makeConstraints { (make) in

                make.right.equalToSuperview().offset(-5)

                make.top.equalToSuperview().offset(5)

                make.bottom.equalToSuperview().offset(-5)

                make.width.equalTo(180)

            }

            

            self.imageView.addSubview(self.label)

            self.label.snp.makeConstraints { (make) in

                make.right.equalToSuperview().offset(-5)

                make.height.width.equalTo(10)

                make.centerY.equalToSuperview()

            }

        }

        

        required init?(coder aDecoder: NSCoder) {

            super.init(coder: aDecoder)

        }

        

        var multidimensionalRankVos:MultidimensionalRankVosModel? {

        didSet {

            guard let model = multidimensionalRankVos else { return }

                self.titleLabel.text = model.dimensionName

            let num:Int = (model.ranks?.count)!

            let margin:CGFloat = 50

            for index in 0..<num {

                    let picView = UIImageView.init(frame: CGRect(x:margin*CGFloat(index)+5*CGFloat(index),y:5,margin,height:margin))

                    picView.layer.masksToBounds = true

                    picView.layer.cornerRadius = picView.frame.size.width/2

                    picView.kf.setImage(with: URL(string: (model.ranks?[index].coverSmall!)!))

                    self.imageView.addSubview(picView)

                    }

                }

            }

    }

  • 相关阅读:
    SQL中的union和union all区别(转)
    Atitit 学校模式之 天堂模式 目录 1.1. 宗旨,让学生们乐不思蜀 打造人间天堂 2 2. 组织结构 2 2.1. 娱乐事业部 2 2.2. 文艺事业部 2 2.3. 三大金山挖掘(教育 医
    Atitit json序列化工具 JsonParserAtiver 参考 Atitit json序列化原理 序列化是将一个对象变成json格式的字符串,而反序列化是将json格式的字符串变
    Atitit 院系和专业规划 大学里的院系,院一般指的都是学院,比如管理学院、法学院这种,系指的就是院里面的专业,比如中文系、哲学系这种。 中文系、哲学系 土木工程 目录 1. 经济管理学院 2
    目录 1.1. Vue是什么??客户端mvc框架,,功能类似springmvc 1 1.2. Why?为什么使用它?? 1 1.3. 包括哪些组件与内部构成与原理 1 2. Howto 怎么使用 2
    Atitit soa之道 艾提拉著作 SOA概念、技术与设计读书笔记 第3章 理解面向服务 10 第4章 理解面向服务架构 39 第5章 理解服务与微服务的层次 74 第6章 Web服务及微服务的
    Atitit table的读取解析from html by jsoup java html paresr 目录 1. atitit.gui控件的定位与选择器 1 2. 读取表格流程 遍历表格ta
    爬虫的一些知识点 目录 1. 网络爬虫 1 2. 产生背景 垂直领域搜索引擎 2 3. 1 聚焦爬虫工作原理以及关键技术概述 3 4. 涉及技术 3 4.1. 下载网页 一般是通过net api
    《数据算法:Hadoop_Spark大数据处理技巧》艾提拉笔记.docx 第1章二次排序:简介 19 第2章二次排序:详细示例 42 第3章 Top 10 列表 54 第4章左外连接 96 第5
    Atitit 算法原理与导论 目录 1. Attilax总结的有用算法 按用途分类 1 1.1. 排序算法 字符串匹配(String Matching) 1 1.2. 加密算法 编码算法 序列
  • 原文地址:https://www.cnblogs.com/sundaysme/p/10613047.html
Copyright © 2011-2022 走看看