//

//  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. Mysql数据库日志,备份及回滚操作

    一.打开二进制日志配置 : 在Windows系统下,对mysql的my.ini的log-bin等进行配置目录位置时,假设要将log-bin的日志配置到D盘的mysqllog的文件为binlog.则可以 ...

  2. ROS-2 : ROS系统层级结构

    一.ROS文件系统层级 ROS的文件和文件夹按如下层级来组织:

  3. 二 配置数据字典&异步查询客户

    数据字典: 字典表和客户表的关系 配置字典表 配置客户表 Spring管理映射文件 1 字典表和客户表的关系 2 配置字典表 3  配置客户表 4  Spring管理映射文件 异步查询客户: 页面加载 ...

  4. 学习进度-10 python爬虫

    学习爬虫的第一个案例是小说爬虫. 小说爬虫首先是解析小说页面源代码,在页面源代码中可以看到小说每章节的内容链接 爬虫的代码: import requests import re url = 'http ...

  5. Jmeter测试入门——分析HBase访问服务性能瓶颈

    开启HBase服务 新建线程组,设定线程数为10:  设定请求方法和请求参数: 查看请求的返回结果: 查看服务响应的性能分析结果: 可能出问题的地方:Phoenix.数据库连接池(操作Phoenix)

  6. Java虚拟机03(Java虚拟机内存模型)

    根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 其实最需要Java程序员关注的是堆,栈,还有方法区,因为啊: 如果代码又问题的话,可能回出现栈溢出 然后说 ...

  7. [Codeforces #608 div2]1272B Blocks

    Description There are nnn blocks arranged in a row and numbered from left to right, starting from on ...

  8. Spring Boot-运行部署

    Main方法 直接运行启动类main方法 遵循应用程序入口点的Java约定的标准方法.我们的main方法SpringApplication通过调用委托给Spring Boot的类run. Spring ...

  9. 使用linux将一个服务器上的文件或者文件夹复制黏贴到另一个服务器上

    一.复制文件: (1)将本地文件拷贝到远程  scp 文件名 用户名@计算机IP或者计算机名称:远程路径 本地192.168.1.8客户端 scp /root/install.* root@10.12 ...

  10. oracle job不运行,定位问题

    一. job的运行频率设置 1.每天固定时间运行,比如早上8:10分钟:Trunc(Sysdate+1) + (8*60+10)/24*60 2.Toad中提供的: 每天:trunc(sysdate+ ...