用UICollectionView实现上下轮播的案例
//
// 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实现上下轮播的案例的更多相关文章
- 用UICollectionView实现无限轮播图
用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节
- 无缝轮播的案例 及css3无缝轮播案例
无缝轮播的案例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- UICollectionView实现无限轮播
#import "KGNewsController.h"#import "KGNewsCell.h"#import "KGNews.h"#i ...
- js原生代码实现轮播图案例
一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...
- css3实现3D切割轮播图案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JS 移动端轮播图案例
body { margin:; } .hearder { width: 100%; height: 150px; position: relative; } ul { list-style: none ...
- jQuery制作焦点图(轮播图)
焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- 原生Javascript实现图片轮播效果
首先引入js运动框架 function getStyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; } else{ ...
- iOS-UICollectionView快速构造/拖拽重排/轮播实现
代码地址如下:http://www.demodashi.com/demo/11366.html 目录 UICollectionView的定义 UICollectionView快速构建GridView网 ...
随机推荐
- Jenkins + git + maven 安装
1.jenkins安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo ...
- 创建用户(adduser和useradd)和删除用户(userdel)及
一 用户创建命令: # adduser 用户名 # useradd 用户名 1) useradd 与 adduser 的区别 在CentOs系统中: useradd与adduser是没有区别的, ...
- Redis的下载与安装
1). 从官网上下载Redis的压缩包 2). 将压缩包解压到 某个指定的文件目录中 tar -xzvf redis-4.0.9.tar.gz /xx/xx/xx_dir 3 ...
- zabbix java gateway配置实战案例
zabbix java gateway配置实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.部署tomcat服务 博主推荐阅读: CentOS: https://www. ...
- ubuntu18.04下neo4j的安装
参考CSDN博客 安装jdk8方式与博客中有不同,按照博客中方法没有成功 以下方法配置环境变量成功 进入配置文件 [root@cuierdan java]# vim /etc/profile在文件的后 ...
- Express 应用程序生成器
通过应用生成器工具 express-generator 可以快速创建一个应用的骨架. express-generator 包含了 express 命令行工具.通过如下命令即可安装: $ npm ins ...
- 用JS写一个网站树形菜单
先上效果图: 主体内容就是侧边展示的一二三级菜单,树形结构的. 前端页面布局内容,页面内容简单用ul li 来完成所有的罗列项.用先后顺序来区分一级二级三级: <body> <b&g ...
- 新闻类网站的通用爬虫--GNE
GNE(GeneralNewsExtractor)是一个通用新闻网站正文抽取模块,输入一篇新闻网页的 HTML, 输出正文内容.标题.作者.发布时间.正文中的图片地址和正文所在的标签源代码.GNE在提 ...
- 使用delphi TThread类创建线程备忘录
备忘,不常用经常忘了细节 TMyThread = class(TThread) private { Private declarations } protected procedure Execute ...
- Ubuntu19.04的安装过程详解以及操作系统初始化配置
Ubuntu19.04的安装过程详解以及操作系统初始化配置 ...