iOS 開發商品詳情頁中的banner中點擊查看圖片

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

輪翻播放與查看是分開的,輪翻是是用 開源的SDCycleScrollView swift

這裏是給出的是查看的:app

 
  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.y - StatusAndNavHeight, width: fr!.width, height: fr!.height)        UIView.animateWithDuration(10, animations: {            
  87.              self.middleImageView?.frame = CGRect(x: fr!.origin.x , 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. }

如何使用:ide

 
  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.     }
相關文章
相關標籤/搜索