用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)
}
}
最新文章
- opencv嫁接vlfeat densesift
- IOS常用的系统文件目录介绍
- Best Time to Buy and Sell Stock II [LeetCode]
- SQL server2012连接不上
- nginx图片过滤处理模块http_image_filter_module安装配置笔记
- Python开发【第十三篇】:jQuery(二)
- 党建凯,创新工场知乎团队Web前端工程师
- time.setToNow() 取当前时间,月份有误
- uml系列(八)——部署图与构件图
- java初级开发程序员(第五单元)
- 15.C++-操作符重载
- iOS开发之二:UIWindow与UIView
- css3的特性
- AI - 参考消息(References)
- C# Redis安装 使用教程
- jquery中ajax使用
- linux audit审计(3)--audit服务配置
- VCC、VDD和VSS
- JNDI 与 LDAP
- docker微服务部署之:六、Rancher管理部署微服务
热门文章
- 安装ruby的一些坑
- 微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
- django ajax发送post请求
- Java生鲜电商平台-优惠券系统设计详解
- HDU 4960 Handling the past 2014 多校9 线段树
- 编译安装常用包+阿里镜像源-常用资源-系统-下载-科莱软件下载-docker仓库包-安全圈-杏雨梨云-图形界面安装-docker私有双仓库-阿里源报错处理-centos7目录大小
- 获取目录结构,并写到txt文档里
- 微信浏览器 UA
- 【剑指Offer】面试题27. 二叉树的镜像
- python学习笔记2018-9-17