swift之无限自动轮播(一)

来源:互联网 发布:linux system接口 编辑:程序博客网 时间:2024/04/29 23:50

这里实现无限轮播采用的是ScrollView+ImageView的方法,还有另一个使用UICollectionView的方法,会放在下一篇进行介绍

下面是主要思路

使用三个imageview控件实现多张image的无限滚动点击图片屏幕始终显示中间的ImageView,左边和右边的ImageView分别代表前一张图片和后一张图片,屏幕移动的时候,中间的ImageView变化,同时左右两边的ImageView也随之变化,两种边界情况:

      (1)当屏幕显示最后一张图片时,右边的ImageView显示最开始的第一张图片;

      (2)当屏幕显示最开始的第一张图片时,左边的ImageView显示最后一张图片。如下代码

 leftIndex = (currentIndex - 1 + 7) % 7//防止越界        rightIndex = (currentIndex + 1) % 7


下面是全部的代码

import UIKitclass ViewController: UIViewController , UIScrollViewDelegate{    var scroll = UIScrollView()    let width = UIScreen.main.bounds.size.width    let height = UIScreen.main.bounds.size.height    var currentIndex : NSInteger = 0    var left = UIImageView()//上一个imageView    var right = UIImageView()// 下一个imageView    var current = UIImageView() // 当前imageView    var imageArray = [String]()    var timer = Timer()    var pageControl = UIPageControl()    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        scroll.contentSize = CGSize(width: width * 3, height: height)        scroll.contentOffset = CGPoint(x: width, y: 0)        scroll.isPagingEnabled = true        imageArray = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg"]        scroll.frame = CGRect(x: 0, y: 0, width: width, height: height)//初始化scrollView 大小为屏幕大小        right.frame = CGRect(x: width * 2, y: 0, width: width, height: height)        current.frame = CGRect(x: width, y: 0, width: width, height: height)        left.frame = CGRect(x: 0, y: 0, width: width, height: height)        scroll.addSubview(right)        scroll.addSubview(current)        scroll.addSubview(left)        scroll.delegate = self        reloadImage()        pageControl.frame = CGRect(x: 0, y: height-100, width: width, height: 100)        pageControl.backgroundColor = UIColor.gray        pageControl.currentPageIndicatorTintColor = UIColor.black        pageControl.pageIndicatorTintColor = UIColor.orange        pageControl.numberOfPages = 7        pageControl.currentPage = 0        setupTimer()        self.view.addSubview(scroll)        self.view.addSubview(pageControl)    }    func reloadImage(){        var leftIndex = 0        var rightIndex = 0        currentIndex = currentIndex % 7        scroll.setContentOffset(CGPoint(x: width, y: 0), animated: false)        pageControl.currentPage = (currentIndex - 1 + 7) % 7//防止越界        leftIndex = (currentIndex - 1 + 7) % 7//防止越界        rightIndex = (currentIndex + 1) % 7        right.image = UIImage(named: imageArray[rightIndex])        current.image = UIImage(named: imageArray[currentIndex])        left.image = UIImage(named: imageArray[leftIndex])    }    //设置定时器    func setupTimer() {        timer = Timer.scheduledTimer(timeInterval: 2,target:self,selector:#selector(timeChanged),userInfo:nil,repeats:true)        RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)    }        func timeChanged(){        currentIndex = currentIndex + 1//更新加载试图        reloadImage()    }                //开始拖动    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {     timer.invalidate()    }    //停止拖动    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {               //向右拖动        if scroll.contentOffset.x > width {            currentIndex = (currentIndex + 1) % 7        }        //向左拖动        if scroll.contentOffset.x < width{            currentIndex = (currentIndex - 1 + 7) % 7        }        //更新小圆点当前位置        pageControl.currentPage = (currentIndex - 1 + 7) % 7        reloadImage()        setupTimer()    }        



0 0