import UIKit
import Kingfisher class BannerView: UIView,UIScrollViewDelegate{ enum ImageType{
case Image //本地图片
case URL //URL
} //图片水平放置到scrollView上
private var scrollView:UIScrollView = UIScrollView()
//小圆点标识
private var pageControl:UIPageControl = UIPageControl() private var center_image:UIImageView = UIImageView()
private var first_image:UIImageView = UIImageView()
private var second_image:UIImageView = UIImageView()
//图片集合
private var images:Array<String> = []
private var type:ImageType = .Image private var width:CGFloat = 0
private var height:CGFloat = 0 private var currIndex = 0
private var clickBlock :(Int)->Void = {index in} private var timer:Timer? // 默认自动播放 设置为false只能手动滑动
var isAuto = true
// 轮播间隔时间 默认4秒可以自己修改
var interval:Double = 4 override func layoutSubviews() {
super.layoutSubviews()
// self.initLayout()
} 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 == 0){
return
} width = frame.size.width
height = frame.size.height scrollView.frame = self.bounds
scrollView.contentSize = CGSize(width:width * CGFloat(3),height:height)
scrollView.contentOffset = CGPoint(x:width,y:0)
scrollView.isUserInteractionEnabled = true
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
addSubview(scrollView) first_image.frame = CGRect(x:0,y:0,width:width,height:height)
first_image.contentMode = .scaleAspectFill
first_image.isUserInteractionEnabled = true
scrollView.addSubview(first_image) center_image.frame = CGRect(x:width,y:0,width:width,height:height)
center_image.contentMode = .scaleAspectFill
center_image.isUserInteractionEnabled = true
scrollView.addSubview(center_image) second_image.frame = CGRect(x:width * 2.0,y:0,width:width,height:height)
second_image.contentMode = .scaleAspectFill
second_image.isUserInteractionEnabled = true
scrollView.addSubview(second_image) pageControl.center = CGPoint(x:width/2,y:height - CGFloat(15))
pageControl.isEnabled = true
pageControl.numberOfPages = images.count
pageControl.currentPageIndicatorTintColor = UIColor.green
pageControl.pageIndicatorTintColor = UIColor.gray
pageControl.isUserInteractionEnabled = false
addSubview(pageControl) //当前显示的只有 center_image 其他两个只是用来增加滑动时效果而已,不需要添加点击事件
addTapGesWithImage(image: center_image)
if(isAuto){
openTimer()
}
setCurrent(currIndex: 0)
} func setCurrent(currIndex:Int) {
self.currIndex = currIndex if(type == .Image){
center_image.image = UIImage.init(named:images[currIndex])
first_image.image = UIImage.init(named:images[(currIndex - 1 + images.count) % images.count])
second_image.image = UIImage.init(named:images[(currIndex + 1) % images.count])
}else{
center_image.setMyImage(URL: NSURL(string: images[currIndex]))
first_image.setMyImage(URL: NSURL(string: images[(currIndex - 1 + images.count) % images.count]))
second_image.setMyImage(URL: NSURL(string: images[(currIndex + 1) % images.count]))
}
center_image.tag = currIndex
pageControl.currentPage = currIndex
scrollView.setContentOffset(CGPoint(x:width,y:0), animated: false)
} //给图片添加点击手势
private func addTapGesWithImage(image:UIImageView) {
let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
image.isUserInteractionEnabled = true //让控件可以触发交互事件
image.contentMode = .scaleAspectFill
image.clipsToBounds = true //超出父控件的部分不显示
image.addGestureRecognizer(tap)
} //点击图片,调用block
@objc func tap(_ ges:UITapGestureRecognizer) {
clickBlock((ges.view?.tag)!)
} func scrollViewDidScroll(_ scrollView: UIScrollView) { } func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
closeTimer()
} func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
openTimer()
} func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(scrollView.contentOffset.x > 0){
currIndex = (currIndex + 1) % images.count
}else{
currIndex = (currIndex - 1 + images.count) % images.count
}
setCurrent(currIndex: currIndex)
} func openTimer(){
if(isAuto){
if(timer == nil){
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 + 1) % images.count)
}
} func isDisplayInScreen() -> Bool{
if(self.window == nil){
return false
}
return true
} } extension UIImageView{
// Kingfisher的覆盖 好处:1.不用所有界面都去导入 2.如果KingfisherAPI更新或者更换图片加载库可以更方便一些,保持方法名和第一个参数不变,修改一下就能达到目的,比如修改为使用SDWebImage
public func setMyImage(URL: NSURL?,placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil){
kf.setImage(with: URL as? Resource,
placeholder: placeholderImage,
options: optionsInfo,
progressBlock: progressBlock,
completionHandler: completionHandler)
}
}

  

使用方法

@IBOutlet weak var bannerView: BannerView!

bannerView.setImages(images: ["ad01.jpg","ad02.jpg","ad03.jpg","ad04.jpg"]){ (index) in

print(index)

}

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

  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. 初识 swift 封装轮播图

    一.简介 换了一家公司.换了一个环境刚开始来公司自然不能有一丝一毫的放松,每天即使是没有什么工作也是看看这个博客.那个源码.尽量让自己更充实.慢慢的开始写几篇博客记录下自己遇到的一些问题和解决方法.其 ...

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

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

  4. android中广告轮播图总结

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

  5. jquery 广告轮播图

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

  6. swift bannerview 广告轮播图

    class BannerView: UIView,UIScrollViewDelegate{ //图⽚⽔平放置到scrollView上 private var scrollView:UIScrollV ...

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

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

  8. Android广告轮播图实现

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

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

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

随机推荐

  1. POJ1625 Censored! —— AC自动机 + DP + 大数

    题目链接:https://vjudge.net/problem/POJ-1625 Censored! Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  2. Kafka kafka.common.OffsetOutOfRangeException 问题处理

    最近公司的zk的down掉了,  storm job 重启的时候报出 kafka.common.OffsetOutOfRangeException 异常 网上查询了一些朋友的做法, 自己也看了一下代码 ...

  3. 标准兼容HTML5输入框提示信息的插件iHolder_v0.1.06.21.2014_预览版

    由于版面限制,简单说下,详细的内容及在线预览.预览版压缩包,见这里http://levi.cg.am/archives/3507 为什么说是标准兼容: 因为大多数placeholder插件是这样兼容的 ...

  4. ubuntn下 apt的用法和yum的比较(转)

    centos有yum安装软件,Ubuntu有apt工具. apt简单的来说,就是给Ubuntu安装软件的一种命令方式. 一.apt的相关文件 /etc/apt/sources.list 设置软件包的获 ...

  5. Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)

    Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...

  6. BZOJ_1025_[SCOI2009]游戏_DP+置换+数学

    BZOJ_1025_[SCOI2009]游戏_DP+置换 Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按 顺序1 ...

  7. 变废为宝,用旧电脑自己DIY组建 NAS 服务器

    i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...

  8. AtCoder Grand Contest 013 C:Ants on a Circle

    题目传送门:https://agc013.contest.atcoder.jp/tasks/agc013_c 题目翻译 给你一个周长为\(L\)的圆,有\(N\)只蚂蚁在圆上爬,速度为一单位距离每秒. ...

  9. BZOJ1382:[Baltic2001]Mars Maps

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  10. poj2955——括号匹配

    题目:http://poj.org/problem?id=2955 区间DP. 代码如下: #include<iostream> #include<cstdio> #inclu ...