//

//  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. BZOJ1019 汉诺塔/洛谷P4285 [SHOI2008]汉诺塔

    汉诺塔(BZOJ) P4285 [SHOI2008]汉诺塔 居然是省选题,还是DP!(我的DP菜得要死,碰见就丢分) 冥思苦想了1h+ \(\to\) ?! 就是普通的hanoi NOI or HNO ...

  2. CodeForces - 869B The Eternal Immortality

    题意:已知a,b,求的最后一位. 分析: 1.若b-a>=5,则尾数一定为0,因为连续5个数的尾数要么同时包括一个5和一个偶数,要么包括一个0. 2.若b-a<5,直接暴力求即可. #in ...

  3. 【HITB GSEC CTF 2017】1000levels

    https://files.cnblogs.com/files/p4nda/498a3f10-8976-4733-8bdb-30d6f9d9fdad.gz #通过阅读天枢战队大佬们的wp调试的结果 首 ...

  4. mysql 添加索引语句

    1.PRIMARY  KEY(主键索引)        mysql>ALTER  TABLE  `table_name`  ADD  PRIMARY  KEY (  `column`  ) 2. ...

  5. 阿里云安装配置nginx

    一.简介 Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供反向代理服务. 二 .安装 1.准备工作 Nginx ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-fast-forward

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

  7. NO11 SSH故障排查思路和netstat命令

    本章知识相关考试:1.企业场景面试题:Linux系统如何优化?2.企业场景面试题:SSH服务连不上,如何排查?记住回答技巧: 1 ping  2 telnet 客户端ssh工具:SecureCRT,x ...

  8. PHPmyadmin Getshell(10.25 第二十七天)

    PHPmyadmin Getshell的方法(1)show global variables like '% secure-file-priv%' 如果该参数设置为空或者指定的文件夹可以利用,然后写木 ...

  9. 小技巧:使用命令行打开vscode 以及 sublime 工具

    vscode手动打开vscode command + shift + p 打开命令面板(或者点击菜单栏 查看>命令面板)输入 shell 选择 install code command in P ...

  10. ffmpeg “inttypes.h”: No such file or directory

    编译过程:错误一:无法打开包括文件:“inttypes.h”: No such file or directory解决方法:删除之,并在其之前添加如下代码: #if defined(WIN32) &a ...