Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144573
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 547
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-01 08:57
个人简介

一个人的生活 新奇视 新感觉 www.x74.cn

文章分类

全部博文(47)

文章存档

2018年(20)

2017年(25)

2015年(1)

2014年(1)

我的朋友

分类: iOS平台

2018-01-09 11:35:09

本文出自code4app,原文地址:http://www.code4app.com/blog-928210-1694.html

轮翻播放与查看是分开的,轮翻是是用 开源的SDCycleScrollView 

这里是给出的是查看的:

  1. ////  FullScreenShowImageView.swift//  joopic////  Created by jianxiong li on 16/9/27.//  Copyright © 2016年 joobot. All rights reserved.//import Foundation
  2. import UIKit//图片轮播组件代理协议protocol FullScreenShowImageViewDelegate{    //获取数据源
  3.     func galleryDataSource()->[String]    //获取内部scrollerView的宽高尺寸
  4.     func galleryScrollerViewSize()->CGSize
  5.     
  6.     func hiddenForCliked(index:Int)
  7. }//图片轮播组件控制器class FullScreenShowImageView: UIView,UIScrollViewDelegate{    //代理对象
  8.     var delegate : FullScreenShowImageViewDelegate!    
  9.     //屏幕宽度
  10.     let kScreenWidth = BWidth    
  11.     //当前展示的图片索引
  12.     var currentIndex : Int = 0
  13.     
  14.     //数据源
  15.     var dataSource : [String]?    
  16.     //用于轮播的左中右三个image(不管几张图片都是这三个imageView交替使用)
  17.     var leftImageView , middleImageView , rightImageView : UIImageView?    
  18.     //放置imageView的滚动视图
  19.     var scrollerView : UIScrollView?    
  20.     //scrollView的宽和高
  21.     var scrollerViewWidth : CGFloat?
  22.     var scrollerViewHeight : CGFloat?    
  23.     //页控制器(小圆点)
  24.     var pageControl : UIPageControl?    
  25.     //加载指示符(用来当iamgeView还没将图片显示出来时,显示的图片)
  26.     var placeholderImage:UIImage!    
  27.     //自动滚动计时器
  28.     var autoScrollTimer:NSTimer?
  29.     
  30.     init(frame: CGRect,delegate:FullScreenShowImageViewDelegate) {        super.init(frame: frame)        self.delegate = delegate
  31.         praperaUI()
  32.     }
  33.     
  34.     required init?(coder aDecoder: NSCoder) {
  35.         fatalError("init(coder:) has not been implemented")
  36.     }
  37.     
  38.     
  39.     func praperaUI() {        
  40.         //获取并设置scrollerView尺寸
  41.         let size : CGSize = self.delegate.galleryScrollerViewSize()        self.scrollerViewWidth = size.width
  42.         self.scrollerViewHeight = size.height
  43.         
  44.         //获取数据
  45.         self.dataSource =  self.delegate.galleryDataSource()        //设置scrollerView
  46.         self.configureScrollerView()        
  47.         //设置加载指示图片
  48.         self.configurePlaceholder()        
  49.         //设置imageView
  50.         self.configureImageView()        
  51.         //设置页控制器
  52.         self.configurePageController()        
  53.         //设置自动滚动计时器
  54.         //self.configureAutoScrollTimer()
  55.         
  56.         self.backgroundColor = UIColor.blackColor()        
  57.         self.addTapAction()
  58.     }
  59.     
  60.     
  61.     func addTapAction(){        //添加组件的点击事件
  62.         let tap = UITapGestureRecognizer(target: self,
  63.                                          action: #selector(FullScreenShowImageView.handleTapAction(_:)))
  64.         self.addGestureRecognizer(tap)
  65.     }    
  66.     //点击事件响应
  67.     func handleTapAction(tap:UITapGestureRecognizer)->Void{        //获取图片索引值
  68.         self.delegate.hiddenForCliked(self.currentIndex)        self.dismissViewAnimate()
  69.     }
  70.     
  71.     func presentViewAnimate() {
  72.         
  73.         let fr = self.middleImageView?.frame
  74.         self.middleImageView?.frame = CGRect(x: fr!.origin.x, y: 22, width: fr!.width, height: fr!.height)        UIView.animateWithDuration(10, animations: {            
  75.             self.middleImageView?.frame = fr!
  76.             
  77.         }) { (_) in
  78.             
  79.         }
  80.         
  81.     }
  82.     
  83.     func dismissViewAnimate() {
  84.         
  85.         let fr = self.middleImageView?.frame
  86.         self.middleImageView?.frame = CGRect(x: fr!.origin.x, y: fr!.origin.- StatusAndNavHeight, width: fr!.width, height: fr!.height)        UIView.animateWithDuration(10, animations: {            
  87.              self.middleImageView?.frame = CGRect(x: fr!.origin., y: -42, width: fr!.width, height: fr!.height)
  88.             
  89.         }) { (_) in            
  90.             self.hidden = true
  91.             self.middleImageView?.frame = fr!
  92.         }
  93.         
  94.     }  
  95.     //设置scrollerView
  96.     func configureScrollerView(){        self.scrollerView = UIScrollView(frame: CGRect(x: 0,y: 0,
  97.             width: self.scrollerViewWidth!, height: BHeight))        
  98.         self.scrollerView?.backgroundColor = UIColor.blackColor()        self.scrollerView?.delegate = self
  99.         self.scrollerView?.contentSize = CGSize(width: self.scrollerViewWidth! * 3,
  100.                                                 height: BHeight)        //滚动视图内容区域向左偏移一个view的宽度
  101.         self.scrollerView?.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)        self.scrollerView?.pagingEnabled = true
  102.         self.scrollerView?.bounces = false
  103.         self.addSubview(self.scrollerView!)
  104.         
  105.     }    
  106.     //设置加载指示图片
  107.     func configurePlaceholder(){        //这里我使用ImageHelper将文字转换成图片,作为加载指示符
  108.         let font = UIFont.systemFontOfSize(17)// UIFont.systemFont(ofSize: 17.0, weight: UIFontWeightMedium)
  109.         let size = CGSize(width: self.scrollerViewWidth!, height: self.scrollerViewHeight!)
  110.         placeholderImage = UIImage(named: "图片加载中...")
  111.     }    
  112.     //设置imageView
  113.     func configureImageView(){        
  114.         self.leftImageView = UIImageView(frame: CGRect(x: 0, y: (BHeight-scrollerViewHeight!)/2,
  115.             width: self.scrollerViewWidth!, height: self.scrollerViewHeight!))        
  116.         self.middleImageView = UIImageView(frame: CGRect(x: self.scrollerViewWidth!, y: (BHeight-scrollerViewHeight!)/2,
  117.             width: self.scrollerViewWidth!, height: self.scrollerViewHeight! ));        
  118.         self.rightImageView = UIImageView(frame: CGRect(x: 2*self.scrollerViewWidth!, y: (BHeight-scrollerViewHeight!)/2,
  119.             width: self.scrollerViewWidth!, height: self.scrollerViewHeight!));        self.scrollerView?.showsHorizontalScrollIndicator = false
  120.         
  121.         self.leftImageView?.contentMode = UIViewContentMode.ScaleAspectFit
  122.         self.middleImageView?.contentMode = UIViewContentMode.ScaleAspectFit
  123.         self.rightImageView?.contentMode = UIViewContentMode.ScaleAspectFit
  124.         
  125.         //设置初始时左中右三个imageView的图片(分别时数据源中最后一张,第一张,第二张图片)
  126.         if(self.dataSource?.count != 0){
  127.             resetImageViewSource()
  128.         }        
  129.         self.scrollerView?.addSubview(self.leftImageView!)        self.scrollerView?.addSubview(self.middleImageView!)        self.scrollerView?.addSubview(self.rightImageView!)
  130.     }    
  131.     //设置页控制器
  132.     func configurePageController() {        self.pageControl = UIPageControl(frame: CGRect(x: kScreenWidth/2-60,
  133.                                                            y: BHeight - 30, width: 120, height: 20))        self.pageControl?.numberOfPages = (self.dataSource?.count)!        self.pageControl?.userInteractionEnabled = false
  134.         self.addSubview(self.pageControl!)
  135.     }    
  136.     //设置自动滚动计时器
  137.     func configureAutoScrollTimer() {        //设置一个定时器,每三秒钟滚动一次
  138.         autoScrollTimer  = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(SliderGalleryController.letItScroll), userInfo: nil, repeats: true)
  139.     }    
  140.     //计时器时间一到,滚动一张图片
  141.     func letItScroll(){
  142.         let offset = CGPoint(x: 2*scrollerViewWidth!, y: 0)        self.scrollerView?.setContentOffset(offset, animated: true)
  143.     }    
  144.     //每当滚动后重新设置各个imageView的图片
  145.     func resetImageViewSource() {        //当前显示的是第一张图片
  146.         if self.currentIndex == 0 {            self.leftImageView?.sd_setImageWithURL(NSURL(string: self.dataSource!.last!))            self.middleImageView?.sd_setImageWithURL(NSURL(string: self.dataSource!.first!))
  147.             let rightImageIndex = (self.dataSource?.count)!>1 ? 1 : 0 //保护
  148.             self.rightImageView?.sd_setImageWithURL(NSURL(string: self.dataSource![rightImageIndex]))
  149.             
  150.             
  151.         }            //当前显示的是最后一张图片
  152.         else if self.currentIndex == (self.dataSource?.count)! - 1 {            
  153.             self.leftImageView?.sd_setImageWithURL(NSURL(string:self.dataSource![self.currentIndex-1]))            self.middleImageView?.sd_setImageWithURL(NSURL(string: self.dataSource!.last!))            self.rightImageView?.sd_setImageWithURL(NSURL(string: self.dataSource!.first!))
  154.             
  155.         }            //其他情况
  156.         else{            
  157.             self.leftImageView?.sd_setImageWithURL(NSURL(string:self.dataSource![self.currentIndex-1]))            self.middleImageView?.sd_setImageWithURL(NSURL(string: self.dataSource![self.currentIndex]))            self.rightImageView?.sd_setImageWithURL(NSURL(string: self.dataSource![self.currentIndex+1]))
  158.         }        
  159.         //设置页控制器当前页码
  160.         self.pageControl?.currentPage = self.currentIndex
  161.     }    
  162.     
  163.     //scrollView滚动完毕后触发
  164.     func scrollViewDidScroll(scrollView: UIScrollView) {        //获取当前偏移量
  165.         let offset = scrollView.contentOffset.x
  166.         
  167.         if(self.dataSource?.count != 0){            
  168.             //如果向左滑动(显示下一张)
  169.             if(offset >= self.scrollerViewWidth!*2){                //还原偏移量
  170.                 scrollView.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)                //视图索引+1
  171.                 self.currentIndex = self.currentIndex + 1
  172.                 
  173.                 if self.currentIndex == self.dataSource?.count {                    self.currentIndex = 0
  174.                 }
  175.             }            
  176.             //如果向右滑动(显示上一张)
  177.             if(offset <= 0){                //还原偏移量
  178.                 scrollView.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)                //视图索引-1
  179.                 self.currentIndex = self.currentIndex - 1
  180.                 
  181.                 if self.currentIndex == -1 {                    self.currentIndex = (self.dataSource?.count)! - 1
  182.                 }
  183.             }            
  184.             //重新设置各个imageView的图片
  185.             resetImageViewSource()
  186.             
  187.         }
  188.     }    
  189.     //手动拖拽滚动开始
  190.     func scrollViewWillBeginDragging(scrollView: UIScrollView) {        //使自动滚动计时器失效(防止用户手动移动图片的时候这边也在自动滚动)
  191.         //autoScrollTimer?.invalidate()
  192.     }    
  193.     //手动拖拽滚动结束
  194.     func scrollViewDidEndDragging(scrollView: UIScrollView,
  195.                                   willDecelerate decelerate: Bool) {        //重新启动自动滚动计时器
  196.         //configureAutoScrollTimer()
  197.         
  198.     }
  199. }

如何使用:

  1. var sliderGallery : FullScreenShowImageView!    var bannerCurrentIndex:Int = 0
  2.     
  3.     //图片轮播组件协议方法:获取内部scrollView尺寸
  4.     func galleryScrollerViewSize() -> CGSize {        return CGSize(width: BWidth, height: BHeight/2)
  5.     }    
  6.     //图片轮播组件协议方法:获取数据集合
  7.     func galleryDataSource() -> [String] {        return self.bannerView.imageURLStringsGroup as! [String]
  8.     }    
  9.      //点击事件响应
  10.     func hiddenForCliked(index:Int){        if(bannerCurrentIndex != index){             self.bannerView.scrollToIndex(Int32(index))
  11.         }        self.navigationController?.setNavigationBarHidden(false, animated: false)
  12.     }
  13.     
  14.     func showImageGallery(index:Int){        //初始化图片轮播组件
  15.         if(sliderGallery == nil){
  16.             sliderGallery = FullScreenShowImageView(frame: CGRect(x: 0, y: 0, width: BWidth,
  17.                 height: BHeight),delegate:self)
  18.             
  19.             sliderGallery.currentIndex = index
  20.             sliderGallery.resetImageViewSource()            
  21.             //将图片轮播组件添加到当前视图
  22.             self.view.addSubview(sliderGallery)
  23.             
  24.         }else{
  25.             sliderGallery.currentIndex = index
  26.             sliderGallery.resetImageViewSource()
  27.             sliderGallery.hidden = false
  28.             
  29.         }        self.sliderGallery.presentViewAnimate()        self.navigationController?.setNavigationBarHidden(true, animated: false)
  30.     }    
  31.     //pragma -- SDCycleScrollViewDelegate
  32.     func cycleScrollView(cycleScrollView: SDCycleScrollView!, didSelectItemAtIndex index: Int) {        print("--------index:(index)")
  33.         bannerCurrentIndex = index       self.showImageGallery(index)
  34.         
  35.     }
阅读(1954) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~