class BannerView: UIView,UIScrollViewDelegate{

 //图⽚⽔平放置到scrollView上
private var scrollView:UIScrollView = UIScrollView()
//⼩圆点标识
private var pageControl:UIPageControl = UIPageControl()
private var imageViews:Array = Array<UIImageView>() //图⽚集合
private var images:Array<String> = []
private var type:ImageType? private var width:CGFloat =
private var height:CGFloat = private var currIndex =
private var clickBlock :(Int)->Void = {index in} private var timer:Timer? // 默认⾃动播放 设置为false只能⼿动滑动
var isAuto = true
// 轮播间隔时间 默认6秒可以⾃⼰修改
var interval:Double = private var startOffsetX:CGFloat = override func layoutSubviews() {
super.layoutSubviews()
} public func setImages(images:Array<String>,type:ImageType
= .Image,imageClickBlock:@escaping (Int) -> Void) {
self.type = type
self.images = images
self.clickBlock = imageClickBlock
self.initLayout()
} private func initLayout(){
if(self.images.count == ){
return
} width = self.bounds.width
height = self.bounds.height scrollView.frame = self.bounds
scrollView.contentSize = CGSize(width:width * CGFloat(images.count +
),height:height)
scrollView.contentOffset = CGPoint(x:width,y:)
scrollView.isUserInteractionEnabled = true
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
self.addSubview(scrollView) var image = UIImageView()
image.frame = CGRect(x:,y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
setImage(image: image, index: images.count - )
scrollView.addSubview(image)
for i in ... images.count{
let image = UIImageView()
image.frame = CGRect(x:width *
CGFloat(i),y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
scrollView.addSubview(image)
setImage(image: image, index: i - )
addTapGesWithImage(image: image)
}
image = UIImageView()
image.frame = CGRect(x:width * CGFloat(images.count +
),y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
scrollView.addSubview(image)
setImage(image: image, index: ) pageControl.center = CGPoint(x:width/,y:height - CGFloat())
pageControl.isEnabled = true
pageControl.numberOfPages = images.count
pageControl.currentPageIndicatorTintColor = UIColor.green
pageControl.pageIndicatorTintColor = UIColor.gray
pageControl.isUserInteractionEnabled = false
self.addSubview(pageControl)
if(isAuto){
openTimer()
}
setCurrent(currIndex: )
} private func setImage(image:UIImageView,index:Int){
if(type == .Image){
image.image = UIImage.init(named:images[index])
}else{
image.setMyImage(url: images[index])
}
} func setCurrent(currIndex:Int) {
if(currIndex < ){
self.currIndex = images.count -
}else{
self.currIndex = currIndex
}
pageControl.currentPage = self.currIndex
scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex +
),y:), animated: false)
} //给图⽚添加点击⼿势
private func addTapGesWithImage(image:UIImageView) {
let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
image.isUserInteractionEnabled = true //让控件可以触发交互事件
image.contentMode = .scaleToFill
// image.clipsToBounds = true //超出⽗控件的部分不显示
image.addGestureRecognizer(tap)
} //点击图⽚,调⽤block
@objc func tap(_ ges:UITapGestureRecognizer) {
clickBlock((ges.view?.tag)!)
} func scrollViewDidScroll(_ scrollView: UIScrollView) { } func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
startOffsetX = scrollView.contentOffset.x
closeTimer()
} func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate
decelerate: Bool) {
} func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(scrollView.contentOffset.x > startOffsetX){
currIndex = (currIndex + ) % images.count
}else{
currIndex = (currIndex - ) % images.count
}
setCurrent(currIndex: currIndex)
openTimer()
} func openTimer(){
if(isAuto){
closeTimer()
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector:
#selector(startAutoScroll), userInfo: nil, repeats: true)
}
} func closeTimer(){
if(timer != nil){
timer?.invalidate()
timer = nil
}
} @objc func startAutoScroll(){
if(isDisplayInScreen()){
setCurrent(currIndex: (currIndex + ) % images.count)
}
} func isDisplayInScreen() -> Bool{
if(self.window == nil){
return false
}
return true
} }
enum ImageType{
case Image //本地图⽚
case URL //URL
}
  

swift bannerview 广告轮播图的更多相关文章

  1. iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  2. JavaScripts广告轮播图以及定时弹出和定时隐藏广告

    轮播图: 函数绑定在body标签内 采用3张图,1.jpg   2.jpg  3.jpg  利用定时任务执行设置图片属性 src  利用for循环可以完成3秒一次 一替换. 定时弹出广告: 由于bod ...

  3. android中广告轮播图总结

    功能点:无限轮播.指示点跟随.点击响应.实现思路: 1.指示点跟随,指示点通过代码动态添加,数量由图片数量决定. 在viewpager的页面改变监听中,设置点的状态选择器enable,当前页时,set ...

  4. jquery 广告轮播图

    轮播图 /*轮播图基本功能: * 1图片切换 * 1.1图片在中间显示 * 1.2图片淡入淡出 * 2左右各有一个按钮 * 2.1点击左按钮,图片切换上一张 * 2.2点击右按钮,图片切换下一张 * ...

  5. swift 广告轮播图

    import UIKit import Kingfisher class BannerView: UIView,UIScrollViewDelegate{ enum ImageType{ case I ...

  6. android-auto-scroll-view-pager (无限广告轮播图)

    github 地址: https://github.com/Trinea/android-auto-scroll-view-pager Gradle: compile ('cn.trinea.andr ...

  7. Android广告轮播图实现

    先看效果 第一步,布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln ...

  8. Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View

    最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...

  9. 从零开始,搭建博客系统MVC5+EF6搭建框架(5),博客详情页、留言、轮播图管理、右侧统计博文

    一.博客系统进度回顾 上一遍博客介绍到,系统已经实现到了发布以及前台布局展示,接下来就是实现一些,详情页,留言.轮播图管理.右侧博文统计信息实现. 二.博客系统详情页实现 2.1先来看看详情页展示的效 ...

随机推荐

  1. 十、React 父组件传来值的类型控制propTypes、父组件如果不传值defaultProps

    父组件给子组件传值时: 1.defaultProps:父子组件传值中,如果父组件调用子组件的时候不给子组件传值,可以在子组件中使用defaultProps定义的默认值: 2.propTypes:验证父 ...

  2. 两表关联更新数据——oracle

    from testb b where b.id=a.id) ; (where exists(select 1 from testb b where b.id=a.id):如果没有这个条件,不匹配的选项 ...

  3. webpack散记--Typescript

    Typescript 1.js的超集 官网:typescriptlang.org/tslang.cn 来自:微软 安装:官方的  npm i typescript ts-loader --save-d ...

  4. TX2_安装view_team

    TX2上的帐号是:1317149963,dc200820305233 参考网站:https://blog.csdn.net/qq_33512213/article/details/90050792 安 ...

  5. JS ~ Promise 对象

    Promise 对象用于表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值. Promise.all(iterable) 这个方法返回一个新的promise对象,该promise对象在i ...

  6. 19 01 17 Django 模板 返回一个页面

    模板 问题 如何向请求者返回一个漂亮的页面呢? 肯定需要用到html.css,如果想要更炫的效果还要加入js,问题来了,这么一堆字段串全都写到视图中,作为HttpResponse()的参数吗?这样定义 ...

  7. js运用sort对json 数组进行排序

    Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列.sort() 方法可以接受一个 方法为参数. sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组 ...

  8. css常用技巧1

    css绘制三角形 <style> .triangle-box{ margin: 50px auto; height: 300px; width: 500px; box-shadow: 1p ...

  9. 用Python分析淘宝2000款避孕套,得出这些有趣的结论

    数据分析之前我们需要清楚的知道自己想要分析什么东西,也就是先搞清楚我们的目标.在公司可能是公司财报.用户增量变化.产品受欢迎程度.一些报表等等. 那我们今天的目标有哪些呢?我们来看看: ! 分析避孕套 ...

  10. Vue.js(2)- 过滤器

    概念:过滤器本质上就是一个函数,可被用作一些常见的文本格式化. 过滤器只可以用在两个地方:mustache 插值表达式和 v-bind 表达式. 过滤器应该被添加在 JavaScript 表达式的尾部 ...