用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网 ...
随机推荐
- Spring任务调度实战之Quartz Cron Trigger
在Quartz中除了使用最简单的Simple Trigger以外,也可以使用类似Linux上Cron作业的CronTrigger的方式来运行Job,下面是一个小例子: 1. 首先是一个任务类,这个类没 ...
- Uncaught Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 3
前端导入静态页面的时候有一个报错,主要问题是冲突了 Uncaught Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or hi ...
- python 中常见的异常类型汇总
异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基类 ...
- 手机连接jmeter录制脚本测试
1.准备条件 电脑安装好jmeter,准备好一个手机 注意: 电脑和手机连接的网络要一致 手机设置代理协议前要先进入想要抓取的网站: http://39.107.96.138:3000/ 2.jmet ...
- php截取指定两个字符之间的字符串
//截取指定两个字符之间的字符串 public function cut($begin,$end,$str){ $b = mb_strpos($str,$begin) + mb_strlen($beg ...
- 计算机是如何计算的、运行时栈帧分析(神奇i++续)
关于i++的疑问 通过JVM javap -c 查看字节码执行步骤了解了i++之后,衍生了一个问题: int num1=50; num1++*2执行的是imul(将栈顶两int类型数相乘,结果入栈), ...
- 051、Java中使用while循环实现1~100的累加
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- php中常用的加密函数
1.MD5加密: string md5 ( string $str [, bool $raw_output = false ] ) (1)md5()默认情况下以 32 字符十六进制数字形式返回散列值, ...
- MQTT 协议学习:007-Keep Alive 连接保活 与 对应报文(PINGREQ、PINGRESP)
背景 keep alive 是 CONNECT 报文中可变头的一部分. 我们提到过 Broker 需要知道 Client 是否非正常地断开了和它的连接,以发送遗愿消息.实际上 Client 也需要能够 ...
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...