import UIKit

private let CycleCellID = "CycleCellID"

class BannerCycleView: UIView {

var layout  : UICollectionViewFlowLayout!

var collectionView :UICollectionView!

var pageContol : UIPageControl?

var cycleTimer : Timer?

let timeInterval : TimeInterval = 2

var cycleModels : [CycleModel]? {

didSet {

collectionView.reloadData()

pageContol?.numberOfPages = cycleModels?.count ?? 0

let indexPath = IndexPath(item: (cycleModels?.count ?? 0) * 100, section: 0)

collectionView.scrollToItem(at: indexPath, at: .left, animated: false)

removeCycleTimer()

addCycleTimer()

}

}

override init(frame: CGRect) {

super.init(frame: frame)

setupUI()

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

}

extension BannerCycleView {

func  setupUI() {

self.layout = UICollectionViewFlowLayout()

self.layout.itemSize = CGSize(width: kScreenW, height: 200)

self.layout.minimumLineSpacing = 0

self.layout.minimumInteritemSpacing = 0

self.layout.scrollDirection = .horizontal

self.collectionView = UICollectionView.init(frame: self.bounds, collectionViewLayout: self.layout)

self.collectionView.isPagingEnabled = true

self.collectionView.showsHorizontalScrollIndicator = false

self.collectionView.delegate = self

self.collectionView.dataSource = self

self.collectionView .register(CollectionCycleCell.self, forCellWithReuseIdentifier: CycleCellID)

self.collectionView.backgroundColor = UIColor.white

self.addSubview(self.collectionView)

self.pageContol = UIPageControl(frame: CGRect(x: self.collectionView.center.x - 50 , y: 130, width: 100, height: 20))

self.addSubview(self.pageContol!)

}

}

extension BannerCycleView : UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return (cycleModels?.count ?? 0) * 10000

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CycleCellID, for: indexPath) as! CollectionCycleCell

print(cycleModels![indexPath.item % cycleModels!.count])

cell.cycleModel = cycleModels![indexPath.item % cycleModels!.count]

return cell

}

}

extension BannerCycleView : UICollectionViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {

// 计算偏移量

let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5

// 计算页码当前页

pageContol?.currentPage = Int(offsetX / scrollView.bounds.width) % (cycleModels?.count ?? 1)

}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

removeCycleTimer()

}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

addCycleTimer()

}

}

extension BannerCycleView {

fileprivate func addCycleTimer(){

cycleTimer = Timer(timeInterval: 2.0, target: self, selector: #selector(self.scrollToNext), userInfo: nil, repeats: true)

RunLoop.main.add(cycleTimer!, forMode: .commonModes)

}

fileprivate func removeCycleTimer(){

cycleTimer?.invalidate()

cycleTimer = nil

}

@objc fileprivate func scrollToNext() {

collectionView.setContentOffset(CGPoint(x:collectionView.contentOffset.x + collectionView.bounds.width , y:0), animated: true)

}

}

// ------ CollectionCycleCell

import UIKit

class CollectionCycleCell: UICollectionViewCell {

var iconImageView: UIImageView!

var titleStringLabel : UILabel!

func setupUI() {

self.backgroundColor = UIColor.white

self.iconImageView = UIImageView()

self.iconImageView.frame = CGRect(x: 0, y: 0, width: kScreenW, height: 200)

self.titleStringLabel = UILabel()

self.titleStringLabel.text = ""

self.titleStringLabel.font = UIFont.systemFont(ofSize: 13)

self.titleStringLabel.frame = CGRect(x:  0, y:  163, width: iconImageView.frame.width, height: 20)

self.titleStringLabel.backgroundColor = UIColor.lightGray.withAlphaComponent(0.8)

self.addSubview(iconImageView)

self.iconImageView.addSubview(self.titleStringLabel)

}

override init(frame: CGRect) {

super.init(frame: frame)

setupUI()

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

// MARK: 定义模型属性

var cycleModel : CycleModel? {

didSet {

titleStringLabel.text = cycleModel?.title

let iconURL = URL(string: cycleModel?.bigimg ?? "")!

iconImageView.kf.setImage(with: iconURL, placeholder: UIImage(named: "homepage_refresh_tv"))

}

}

}

//---- model

import UIKit

class CycleModel: NSObject {

var title : String = ""

var smallimg : String = ""

var bigimg : String = ""

init(dict : [String : Any]) {

super.init()

setValuesForKeys(dict)

}

override func setValue(_ value: Any?, forUndefinedKey key: String) {}

}

把埋怨的话放一放,把负能量收一收。太阳喜欢向日葵,人们也会喜欢上爱笑的你。习惯好的自己,一切都会好的。

转载于:https://www.cnblogs.com/supersr/p/5519251.html

swift轮播图代码相关推荐

  1. bootstrap轮播图代码详解

    最近在做个人简历网站,然后要用到轮播图,上网查了一下,以下为轮播图代码: 下面展示一些 内联代码片. <!-- 轮播(Carousel)项目 --> <div id="my ...

  2. JavaScript轮播图代码

    JavaScript轮播图代码 <html><head><meta charset="utf-8"><title>轮播图</t ...

  3. js轮播图代码_javascript基础(一)——轮播图

    javascript基础(一)--轮播图 1.轮播图的实现原理 轮播图的js实现原理其实十分的简单,首先将图片放入一个ul标签中,ul标签的大小要足够将所有图片放入其中.然后再将ul标签放进div中, ...

  4. 微信小程序 轮播图代码

    微信小程序轮播图的代码如下: 首先,在小程序的 wxml 文件中,添加如下代码: <!-- 轮播图组件 --> <swiper class="swiper" in ...

  5. 左右无缝轮播html,JS实现左右无缝轮播图代码

    废话不多说了,直接给大家贴代码了. 无缝轮播图: 无缝轮播图 *{margin: 0;padding:0; } ul{list-style: none;} .banner{width: 600px;h ...

  6. 【JS 纯手写轮播图代码】

    轮播图实现 首先需要在同级目录下创建img文件夹,用以储存你需要轮播的图片,注意设置好图片的宽度,以免出现空白区域.然后就可以愉快地实现轮播功能啦~ // An highlighted block & ...

  7. 用Android studio写一个轮播图代码

    好的,下面是一个使用Android Studio编写轮播图的示例代码: 首先,在你的布局文件中添加一个ViewPager和一个指示器(例如使用ViewPagerIndicator库),用于显示轮播图和 ...

  8. 【学习笔记57】轮播图代码的实现

    一.HTML代码 <div class="banner"><!-- 放置所有轮播图的盒子 --><ul class="img_box&quo ...

  9. php实现轮播图代码,微信小程序实例:轮播图的代码实现与分析

    本篇文章给大家带来的内容是关于微信小程序实例:轮播图的代码实现与分析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 本文来说说轮播图,最常见的一个轮播图,中间带小圆点,自动轮播. S ...

最新文章

  1. Crunch Bang(linux)安装Webstorm上手
  2. 产品中心和用户中心的视角摘抄
  3. 让电脑开机时直接登录
  4. 几种jvm OOM问题
  5. 高等数学上-赵立军-北京大学出版社-题解-练习5.7
  6. C#相关基础知识点总结+基础代码
  7. java 项目做多级缓存_【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)...
  8. linux用户管理命令 2
  9. Jmeter的基本功能使用方法
  10. android MIT App Inventor 2 Beta 中文资料
  11. Java 基于JavaMail实现向QQ邮箱发送邮件(未测试)
  12. cityengine常用规则函数
  13. 相同间距下,相邻层间耦合带来影响比同层间耦合更严重
  14. 更换网站logo,超简单方法
  15. CleanMyMac X国外中文注册激活版Mac系统清理优化工具
  16. 极品,git简介,安装,方法
  17. Linux琐屑下Resin JSP MySQL的布置和设置配备陈设-2
  18. 仿写某品牌手机官网(静态)
  19. 如何成为出色的演讲家
  20. Python玩转Excel:统计函数

热门文章

  1. Linux进程与线程的区别 详细总结(面试经验总结)
  2. PHP 预编译加速: eAccelerator的安装和性能比较
  3. PHP autoload机制详解
  4. ASP.NET实现身份模拟
  5. javascript页面跳转常用代码
  6. 朴素、Select、Poll和Epoll网络编程模型实现和分析——朴素模型
  7. 吴恩达老师深度学习视频课笔记:超参数调试、Batch正则化和程序框架
  8. travis-ci如何配置android
  9. 手机贷服务器维护升级,手机贷服务器升级
  10. 河南科技大学c语言章节作业答案,河南科技大学C语言试题