//

//  RecommendNewsCell.swift

//  XMLYFM

//

//  Created by Domo on 2018/8/2.

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

//

import UIKit

class RecommendNewsCell: UICollectionViewCell {

private var topBuzz:[TopBuzzModel]?

private lazy var imageView : UIImageView = {

let imageView = UIImageView()

imageView.image = UIImage(named: "news.png")

return imageView

}()

private var moreBtn:UIButton = {

let button = UIButton.init(type: UIButtonType.custom)

button.setTitle("|  更多", for: UIControlState.normal)

button.setTitleColor(UIColor.gray, for: UIControlState.normal)

button.titleLabel?.font = UIFont.systemFont(ofSize: 15)

return button

}()

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-150), height:40)

let collectionView = UICollectionView.init(frame:CGRect(x:80,y:5, width:YYScreenWidth-150, height:40), collectionViewLayout: layout)

layout.scrollDirection = UICollectionViewScrollDirection.vertical

collectionView.contentSize = CGSize(width: (YYScreenWidth-150), height: 40)

collectionView.delegate = self

collectionView.dataSource = self

collectionView.backgroundColor = UIColor.white

collectionView.showsVerticalScrollIndicator = false

collectionView.showsHorizontalScrollIndicator = false

collectionView.isPagingEnabled = true

collectionView.isScrollEnabled = false

collectionView.register(NewsCell.self, forCellWithReuseIdentifier:"NewsCell")

return collectionView

}()

var timer: Timer?

override init(frame: CGRect) {

super.init(frame: frame)

setUpUI()

// 开启定时器

starTimer()

}

func setUpUI(){

self.addSubview(self.imageView)

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

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

make.width.equalTo(60)

make.height.equalTo(30)

make.top.equalTo(10)

}

self.addSubview(self.moreBtn)

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

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

make.width.equalTo(60)

make.height.equalTo(40)

make.top.equalTo(5)

}

self.addSubview(self.collectionView)

}

var topBuzzList:[TopBuzzModel]? {

didSet{

guard let model = topBuzzList else { return }

self.topBuzz = model

self.collectionView.reloadData()

}

}

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

}

}

extension RecommendNewsCell : UICollectionViewDelegate, UICollectionViewDataSource {

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

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

}

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

let cell:NewsCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewsCell", for: indexPath) as! NewsCell

cell.titleLabel.text = self.topBuzzList?[indexPath.row%(self.topBuzz?.count)!].title

return cell

}

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

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

}

/// 开启定时器

func starTimer () {

let timer = Timer.init(timeInterval: 2, 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 currentOffsetY = collectionView.contentOffset.y

let offsetY = currentOffsetY + collectionView.bounds.height

// 2.滚动该位置

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

}

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

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

self.timer?.invalidate()

self.timer = nil

}

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

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

starTimer()

}

}

class NewsCell: UICollectionViewCell {

lazy var titleLabel : UILabel = {

let label = UILabel()

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

return label

}()

override init(frame: CGRect) {

super.init(frame: frame)

self.addSubview(self.titleLabel)

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

make.left.right.height.top.equalToSuperview()

}

}

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

}

}

用UICollectionView实现上下轮播的案例的更多相关文章

  1. 用UICollectionView实现无限轮播图

    用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节

  2. 无缝轮播的案例 及css3无缝轮播案例

    无缝轮播的案例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. UICollectionView实现无限轮播

    #import "KGNewsController.h"#import "KGNewsCell.h"#import "KGNews.h"#i ...

  4. js原生代码实现轮播图案例

    一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...

  5. css3实现3D切割轮播图案例

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

  6. JS 移动端轮播图案例

    body { margin:; } .hearder { width: 100%; height: 150px; position: relative; } ul { list-style: none ...

  7. jQuery制作焦点图(轮播图)

    焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  8. 原生Javascript实现图片轮播效果

    首先引入js运动框架 function getStyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; } else{ ...

  9. iOS-UICollectionView快速构造/拖拽重排/轮播实现

    代码地址如下:http://www.demodashi.com/demo/11366.html 目录 UICollectionView的定义 UICollectionView快速构建GridView网 ...

随机推荐

  1. 码云上部署hexo博客框架

    title: 码云上部署hexo博客框架 Hexo框架在码云上实现个人博客 本文受 https://www.jianshu.com/p/84ae2ba1c133 启发编写 本地调试 安装完Node.j ...

  2. ReentrantLock售票的例子&sleep和wait的区别锁可重入是什么(笔记)

    1 sleep 在哪里都可以用 调用Thread.sleep()但是 wait方法只能在同步方法和同步代码块中使用 wait也就是使得该线程成为阻塞状态(注意这里阻塞不是书本操作系统下的while循环 ...

  3. CF755G PolandBall and Many Other Balls 题解

    从神 Karry 的题单过来的,然后自己瞎 yy 了一个方法,看题解区里没有,便来写一个题解 一个常数和复杂度都很大的题解 令 \(dp_{i,j}\) 为 在 \(i\) 个球中选 \(j\) 组的 ...

  4. 一 Mybatis概述&与Hibernate的区别&CRUD

    Mybatis是类似Hibernate的ORM持久层框架 为什么学习Mybatis? 是目前国内主流的持久层框架,面向sql(相较于Hibernate,Mybatis一定要用sql) Hibernat ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮标签

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. js 转换时间戳为时间格式并且按指定格式输出

    /** * 时间戳转换为日期 */ function convertTimestamp(timestamp){ // 时间戳转换为日期 var d = new Date(timestamp); // ...

  7. 第1节 HUE:13、hue的下载以及安装配置

    hue的基本介绍:主要是用于与其他各个框架做整合的,提供一个web界面可以供我们去操作其他的大数据框架可以理解为这个hue就是一个与其他各个框架整合的工具,hue本身不提供任何的功能,所有的功能,都是 ...

  8. springboot启动不能加载数据库驱动Failed to determine a suitable driver class

    SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/G:/sharp/repo ...

  9. decompiler of java

    运维了两个java项目,但是没有源代码,整天都是各种问题,各方面都不配合.我也只是个小小的兵,但是工作还是要做. 转机 偶然想试一试decomplier,就找到了gd-gui,感觉用着挺好的,到把项目 ...

  10. 最小生成树的两种方法(Kruskal算法和Prim算法)

    关于图的几个概念定义: 连通图:在无向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该有向图为强连 ...