用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网 ...
随机推荐
- 八: IO流,数据的读写传输
IO流概括图: IO流的分类: 按流: 输入流(InputStream和Reader):从硬盘或者别的地方读入内存 输出流(OutputStream和Writer):从内存里向硬盘或别的地方输出 按 ...
- Python @函数装饰器及用法
1.函数装饰器的工作原理 函数装饰器的工作原理是怎样的呢?假设用 funA() 函数装饰器去装饰 funB() 函数,如下所示: #funA 作为装饰器函数 def funA(fn): #... fn ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-muted" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python-模块加密hashlib
import hashlib# md5ybm_pwd='yuanbapqingsdfs234FF234HF@F' #m = hashlib.md5() #bytes_ybq = ybm_pwd.enc ...
- python编写banner获取的常用模块
模块的概念:模块也叫库,每个模块中都内置了大量的功能和函数.类和变量.它就像是积木,可以根据需要进行调用组合.模块就是程序,每个模块就是一个后缀为.py的Python程序.Python的模块分为标准模 ...
- 用 Weave Scope 监控集群【转】
创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其职,有足够的资源满足应用的需求.Kubernetes 是一个复杂系统,运维团 ...
- Express 应用程序生成器
通过应用生成器工具 express-generator 可以快速创建一个应用的骨架. express-generator 包含了 express 命令行工具.通过如下命令即可安装: $ npm ins ...
- C++编程学习(十二) STL
一.简介 标准模板库STL,是一组模板类和函数.提供了: 1.容器.用于存储信息. 2.迭代器.用于访问容器中的信息. 3.算法.操作容器内容. 1.容器 STL有两种类型的容器类: (1)顺序容器 ...
- tools.logback
https://logback.qos.ch/manual/index.html Logback is built upon three main classes: Logger, Appender ...
- Day6 - E - Brownie Points II POJ - 2464
Stan and Ollie play the game of Odd Brownie Points. Some brownie points are located in the plane, at ...