Swift 版 封裝的無(wú)限輪播視圖

此類(lèi)中的代碼,是已經(jīng)封裝好的,只需要提供dataArray數(shù)組中的圖片就可以實(shí)現(xiàn)無(wú)限輪播

原理: 相同兩張圖片之間快速切換,使人眼無(wú)法分辨出,所以就會(huì)產(chǎn)生輪播的錯(cuò)覺(jué)

demo鏈
接:https://github.com/baiGenZhengliu/Swift-.git


import UIKit

class HJZView: UIView ,UIScrollViewDelegate{
    

    let kScreenW = UIScreen.mainScreen().bounds.size.width
    
    var dataArray:NSArray?;
    
    var _scrollView:UIScrollView?;
    
    //計(jì)時(shí)器
    
    var _timer:NSTimer?;
    
    //主要是為了確定scrollview和pagecontroller的顯示位置
    
    var index:Int64?;
    
    var _pageControl:UIPageControl?;
    
    override init(frame: CGRect) {
        
        super.init(frame: frame);
        
        /*
          本代碼只需要改動(dòng)dataArray中的圖片,
          pageControl,會(huì)自動(dòng)根據(jù)數(shù)組中的元素個(gè)數(shù)調(diào)整
        
        */
        
        
        dataArray = ["aion01.jpg","aion02.jpg","aion03.jpg","aion04.jpg","aion05.jpg"];
        //
        
        //創(chuàng)建scrollview
        
        _scrollView = UIScrollView(frame: CGRectMake(0, 0, kScreenW,self.bounds.size.height));
        
        _scrollView?.bouncesZoom = false;
        
        _scrollView?.bounces = false;
        
        _scrollView?.showsHorizontalScrollIndicator = false;
        
        _scrollView?.showsVerticalScrollIndicator = false;
        
        _scrollView?.pagingEnabled = true;
        
        _scrollView?.delegate = self;
        
        self.addSubview(_scrollView!);

        _scrollView?.backgroundColor = UIColor.redColor();
        
        //創(chuàng)建pageControl
        
        _pageControl = UIPageControl(frame: CGRectMake(0, self.bounds.size.height - 20, kScreenW, 20));
        
        self.addSubview(_pageControl!);
        
        _pageControl!.backgroundColor = UIColor.clearColor();
        
        _pageControl!.numberOfPages = (dataArray?.count)!;
        
        _pageControl!.currentPage = 0;
        
        _pageControl!.pageIndicatorTintColor = UIColor.whiteColor();
        
        _pageControl!.currentPageIndicatorTintColor = UIColor.orangeColor();
        
        creatImageView();
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //創(chuàng)建scrollview上的圖片控件
    
    func creatImageView(){
        
        index = 0;
        
        var i = 0;
        
        
        //創(chuàng)建第一張圖片,首個(gè)圖片框
        
        let firstimage = UIImage(named: dataArray?.lastObject as! String)
        
        let firstimageview = UIImageView(frame: CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        
        firstimageview.image = firstimage
        
        _scrollView?.addSubview(firstimageview)
        
        for imgNmae in dataArray!{
            
            let imgV = UIImageView(frame: CGRectMake(self.bounds.size.width * CGFloat(i+1), 0, self.bounds.size.width, self.bounds.size.height));
            
            imgV.image = UIImage(named: imgNmae as! String);
            
            _scrollView?.addSubview(imgV);
            
            i++ ;
            
        }
        
        //創(chuàng)建最后一個(gè)圖片框
        
        let lastimage = UIImage(named: dataArray?.firstObject as! String)
        
        let lastimageview = UIImageView(frame: CGRect.init(x: self.bounds.size.width * CGFloat((dataArray?.count)! + 1), y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        
        lastimageview.image = lastimage
        
        _scrollView?.addSubview(lastimageview)
        
        _scrollView?.contentSize = CGSizeMake(self.bounds.size.width * CGFloat((dataArray?.count)! + 2), self.bounds.size.height);
     
        //創(chuàng)建計(jì)時(shí)器
        
        _timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "timeMethod", userInfo: nil, repeats: true);
        
        //創(chuàng)建runloop設(shè)置計(jì)時(shí)器的運(yùn)行模式,保證在滑動(dòng)界面時(shí),計(jì)時(shí)器也在工作
        
        NSRunLoop.currentRunLoop().addTimer(_timer!, forMode: NSRunLoopCommonModes);
        
    }
    
    //計(jì)時(shí)器調(diào)用的方法
    
    func timeMethod(){
        
        index = index!+1;

        if NSInteger(index!)  == ((dataArray?.count)!) + 1{
            
            index = 0;
            
             _scrollView?.contentOffset = CGPointMake(0, 0)
            
            index = 1;
        }
        
        

       
        UIView.animateWithDuration(0.3) { () -> Void in
            
            self._scrollView!.contentOffset = CGPointMake(CGFloat((self.index!)) * self.kScreenW, self._scrollView!.contentOffset.y);
            
        };
        _pageControl!.currentPage = NSInteger(index!);

        if NSInteger(index!)  == ((dataArray?.count)!) {
            
           _pageControl!.currentPage = 0;
            
        }
       
        

    }
    
    
    // scrollView delegate
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let x = scrollView.contentOffset.x/kScreenW;
        
        index = Int64(x);
   
        _pageControl!.currentPage = NSInteger(x);
        
    }
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實(shí)...
    香橙柚子閱讀 24,822評(píng)論 8 183
  • 01 馬云在俄羅斯圣彼得堡國(guó)際經(jīng)濟(jì)論壇上發(fā)言時(shí)說(shuō):“我有生以來(lái)最大的錯(cuò)誤就是創(chuàng)建阿里巴巴?!本W(wǎng)友當(dāng)即表示這個(gè)X裝的...
    永遠(yuǎn)的迷之喵喵閱讀 370評(píng)論 0 5
  • 用淘寶的msui框架寫(xiě)APP界面非常方便,但cordova打包時(shí)圖標(biāo)全都無(wú)法顯示,用Safari跟蹤發(fā)現(xiàn)原來(lái)ico...
    漁歌子閱讀 673評(píng)論 0 0
  • When I pick it up to spend a couple days to read...
    鄉(xiāng)村_歲月閱讀 551評(píng)論 0 0
  • 寒風(fēng)微起頃刻間, 赤熱初心灼狂言, 孤盞獨(dú)明傲骨現(xiàn), 管他俗世夾俗見(jiàn); 肅雨蕭蕭漫天艷, 不畏艱險(xiǎn)展鴻藍(lán), 笑看凡...
    智慧淫者閱讀 460評(píng)論 0 2

友情鏈接更多精彩內(nèi)容