• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swiftbannerview广告轮播图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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 = 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
 // 轮播间隔时间 默认6秒可以⾃⼰修改
 var interval:Double = 6
 
 private var startOffsetX:CGFloat = 0
 
 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 == 0){
 return
 }
 
 width = self.bounds.width
 height = self.bounds.height
 
 scrollView.frame = self.bounds
 scrollView.contentSize = CGSize(width:width * CGFloat(images.count + 
2),height:height)
 scrollView.contentOffset = CGPoint(x:width,y:0)
 scrollView.isUserInteractionEnabled = true
 scrollView.isPagingEnabled = true
 scrollView.showsHorizontalScrollIndicator = false
 scrollView.delegate = self
 self.addSubview(scrollView)
 
 var image = UIImageView()
 image.frame = CGRect(x:0,y:0,width:width,height:height)
 image.contentMode = .scaleToFill
 image.isUserInteractionEnabled = true
 setImage(image: image, index: images.count - 1)
 scrollView.addSubview(image)
 for i in 1 ... images.count{
 let image = UIImageView()
 image.frame = CGRect(x:width * 
CGFloat(i),y:0,width:width,height:height)
 image.contentMode = .scaleToFill
 image.isUserInteractionEnabled = true
 scrollView.addSubview(image)
 setImage(image: image, index: i - 1)
 addTapGesWithImage(image: image)
 }
 image = UIImageView()
 image.frame = CGRect(x:width * CGFloat(images.count + 
1),y:0,width:width,height:height)
 image.contentMode = .scaleToFill
 image.isUserInteractionEnabled = true
 scrollView.addSubview(image)
 setImage(image: image, index: 0)
 
 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
 self.addSubview(pageControl)
 if(isAuto){
 openTimer()
 }
 setCurrent(currIndex: 0)
 }
 
 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 < 0){
 self.currIndex = images.count - 1
 }else{
 self.currIndex = currIndex
 }
 pageControl.currentPage = self.currIndex
 scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex + 
1),y:0), 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 + 1) % images.count
 }else{
 currIndex = (currIndex - 1) % 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 + 1) % images.count)
 }
 }
 
 func isDisplayInScreen() -> Bool{
 if(self.window == nil){
 return false
 }
 return true
 }
 
}
enum ImageType{
 case Image //本地图⽚
 case URL //URL
}
  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift之NSProgressIndicator的简单实用发布时间:2022-07-13
下一篇:
Swift代理造成内存泄漏的解决办法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap