美文网首页iOS-Swift@IT·互联网程序员
Swift 版 封装的无限轮播视图

Swift 版 封装的无限轮播视图

作者: 百舸争流111 | 来源:发表于2016-07-16 20:38 被阅读127次

此类中的代码,是已经封装好的,只需要提供dataArray数组中的图片就可以实现无限轮播

原理: 相同两张图片之间快速切换,使人眼无法分辨出,所以就会产生轮播的错觉

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?;
    
    //计时器
    
    var _timer:NSTimer?;
    
    //主要是为了确定scrollview和pagecontroller的显示位置
    
    var index:Int64?;
    
    var _pageControl:UIPageControl?;
    
    override init(frame: CGRect) {
        
        super.init(frame: frame);
        
        /*
          本代码只需要改动dataArray中的图片,
          pageControl,会自动根据数组中的元素个数调整
        
        */
        
        
        dataArray = ["aion01.jpg","aion02.jpg","aion03.jpg","aion04.jpg","aion05.jpg"];
        //
        
        //创建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();
        
        //创建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")
        
    }
    
    //创建scrollview上的图片控件
    
    func creatImageView(){
        
        index = 0;
        
        var i = 0;
        
        
        //创建第一张图片,首个图片框
        
        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++ ;
            
        }
        
        //创建最后一个图片框
        
        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);
     
        //创建计时器
        
        _timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "timeMethod", userInfo: nil, repeats: true);
        
        //创建runloop设置计时器的运行模式,保证在滑动界面时,计时器也在工作
        
        NSRunLoop.currentRunLoop().addTimer(_timer!, forMode: NSRunLoopCommonModes);
        
    }
    
    //计时器调用的方法
    
    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);
        
    }
    
}

相关文章

  • Swift 版 封装的无限轮播视图

    此类中的代码,是已经封装好的,只需要提供dataArray数组中的图片就可以实现无限轮播 原理: 相同两张图片之间...

  • swift第三方控件

    iOS开发:Swift实现的轮播图、无限循环视图控件CYCircularScrollView CycleScrol...

  • swift 轮播图无限滚动 控件的封装

    swift 轮播图无限滚动 控件的封装 这篇文章主要记录自己是怎么一步一步封装一个 轮播图, 记录了整个过程和遇到...

  • Swift - 封装无限轮播器

    说明: 最近开始新的项目了,不过这次是以为Swift做为新项目的开发语言,而不是OC了,然后在项目中有一些地方用到...

  • 【iOS】Swift实现图片轮播

    使用Swift简单的封装了一个图片轮播视图,如果有人觉得实用的话可以直接拖入项目中简单调用就可以实现图片轮播。由于...

  • SwiftUI实战-轮播图

    无限轮播图 相关源码:ContentView.swift CustomLoopView.swift

  • 面向对象实战

    代码预览 tab组件懒加载组件无限轮播组件无限轮播二次封装组件modal组件日历组件

  • Swift 无限轮播+自动无限轮播

    title: Swift Image browserdate: 2016-10-20 12:46:08catego...

  • ios无限轮播图

    一个非常简单但很实用的无限轮播图(swift版本)UICollectionView无限轮播效果已经有很多前辈定制过...

  • 10.2 简单匀速动画封装

    //封装匀速动画框架 可用于无限轮播 如 京东轮播function constant(obj,speed,ta...

网友评论

    本文标题:Swift 版 封装的无限轮播视图

    本文链接:https://www.haomeiwen.com/subject/lnkujttx.html