
import UIKitimport Kingfisherclass 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,height:height) center_image.contentMode = .scaleAspectFill center_image.isUserInteractionEnabled = true scrollVIEw.addSubvIEw(center_image) second_image.frame = CGRect(x:wIDth * 2.0,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){ 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) }}
使用方法
@H_291_403@@IBOutlet@H_324_404@ @H_291_403@weak@H_324_404@ @H_291_403@var@H_324_404@ bannerVIEw: BannerVIEw!
bannerVIEw.setimages(images: ["ad01.jpg","ad02.jpg","ad03.jpg","ad04.jpg"]){ (index) @H_291_403@in@H_324_404@
print(index)
}
总结以上是内存溢出为你收集整理的swift 广告轮播图全部内容,希望文章能够帮你解决swift 广告轮播图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)