//

//  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(width: (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(width: (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,width: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)

}

}

}

}

swift中使用UIColllectionView实现横向轮播的一般方法的更多相关文章

  1. js 实现横向轮播效果

    参考:https://www.cnblogs.com/LIUYANZUO/p/5679753.html html: <!DOCTYPE html> <html> <hea ...

  2. Bootstrap 历练实例-轮播(carousel)插件方法

    方法 下面是一些轮播(Carousel)插件中有用的方法: 方法 描述 实例 .carousel(options) 初始化轮播为可选的 options 对象,并开始循环项目. $('#identifi ...

  3. React中使用CSSTransitionGroup插件实现轮播图

    动画效果,是一个页面上必不可少的功能,学习一个新的东西,当然就要学习,如何用新的东西,用它的方法去实现以前的东西啦.今天呢,我就在这里介绍一个试用react-addons-css-transition ...

  4. html中使用JS实现图片轮播效果

    1.首先是效果图,要在网页中实现下图的轮播效果,有四张图片,每张图片有自己的标题,然后还有右下角的小方框,鼠标悬浮在小方框上,会切换到对应的图片中去. 2.先是HTML中的内容,最外层是轮播图整个的容 ...

  5. 记一个jquery 无缝轮播的制作方法

    接触前端也很久了,今天才发现,要做好一个轮播,其实有很多东西需要考虑进去,否则做出来的轮播效果并不好,下面我就来做一个轮播,是依赖jquery来写的 1.要做轮播,首先需要的是HTML的内容,css的 ...

  6. 移动端轮播图实现方法(dGun.js)

    本文章介绍在移动端无缝隙轮播图实现的原理,这个轮子比较简单,但可以方便刚刚入门的同学参考.最终效果是在移动端无缝隙无限滑动,可以自定义轮播的速度.支持手势左右滑动.最后会放上源码. HTML部分 &l ...

  7. H5制作显示轮播图的方法Swiper

    1.需要引入Swiper插件 <!-- swiper插件 --> <link rel="stylesheet" href="https://unpkg. ...

  8. swiper横向轮播--3d

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. swiper横向轮播(兼容IE8)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

随机推荐

  1. 机器学习中 为何要使用 独热编码 one-hot

    背景 接触tensorflow时,学习到mnist,发现处理数据的时候采取one-hot编码,想起以前搞FPGA状态机遇到过格雷码与独热码. 解析: 将离散型特征使用one-hot编码,确实会让特征之 ...

  2. ElasticSearch学习,入门篇(一)

    概念解析 1.什么是搜索 搜索就是在任何场景下,找寻你想要的信息,这个时候你会输入一段要搜索的关键字,然后期望找到这个关键字相关的有效信息. 2.如果用数据库做搜素会怎么样 select * from ...

  3. HTML学习第五天

    HTML学习第五天 今天学HTML的实体.背景.布局 HTML布局的标签基本被淘汰frameset就被淘汰了,只有iframe依然存活,但是iframe可以被CSS给代替.下面就是一个练习的程序 &l ...

  4. 最简单-转换MBR为GPT

    Windows Server 2016可能没有这个 mbr2gpt 这个软件,可以从Windows 10 的C:\Windows\System32 目录下面复制到 Windows Server 201 ...

  5. 第四张5G牌照发给广电,能打破三大运营商的垄断吗?

    近段时间,多个国家处于莫须有的安全性考虑,禁止华为参与核心5G网络设备竞标.其实这就从侧面反映出,国内民族企业在5G层面的领先性.当然,这也让我们认知到,5G网络将是新时代的竞争关键节点.为此,国内正 ...

  6. Manacher算法[O(n)]

    问题描述: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段.回文的含义是:正着看和倒着看相同,如abba和yyxyy. 算法基本要点: 首先用一个非常巧妙的方式,将 ...

  7. java#类的实例化顺序

    关于类的实例化,不用弄的那么细致,这里只说单一类,没有其他父类(排除Obejct)的情况.要实例化一个类,需要加载class文件到jvm并且验证通过了是安全的字节码文件. 初始化大致上是按照如下步骤: ...

  8. 010.Delphi插件之QPlugins,遍历服务接口

    这个DEMO注意是用来看一个DLL所拥有的全部服务接口 演示效果如下 代码如下: unit Frm_Main; interface uses Winapi.Windows, Winapi.Messag ...

  9. python进阶 廖雪峰(慕课网)

    1.函数式编程 变量名可以指向函数,那么函数就可以通过一个变量传递给另一个函数或者变量. map()函数:接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到 ...

  10. Python3 格式化输出

    Python3 格式化输出 今天用字符串功能的时候,我突然忘记了格式化输出的方式X﹏X.所以赶紧恶补一下. 1.打印字符串 print("My name is %s" %(&quo ...