UICollectionView实现轮播图

2016-08-30  本文已影响494人  07212a79db66

使用UICollectionView实现一个简单的轮播图效果

轮播图.gif
//
//  ViewController.swift
//  轮播图
//
//  Created by zhaodajun on 16/8/30.
//  Copyright © 2016年 zhaodajun. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var timer:Timer?
    var index:Int = 0
    
    var imagesArray:[String] = ["0.jpg","1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"]
 
    override func viewDidLoad() {
        super.viewDidLoad()

        installUI()
        startTimer()
    }
    
    func installUI()  {
        view.addSubview(collectionView)
        page.numberOfPages = imagesArray.count
        view.addSubview(page)
    }

    
    func startTimer()  {
        timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.scrollToImage), userInfo: nil, repeats: true)
    }
    
    func scrollToImage()  {
        if index == imagesArray.count  {
            index = 0
        }
       
        let  indexPath = NSIndexPath.init(item: self.index, section: 0)
        self.collectionView.scrollToItem(at: indexPath as IndexPath, at: UICollectionViewScrollPosition.init(rawValue: 0), animated: false)
         index += 1
    }
    
    private lazy var collectionView: UICollectionView = {
        let collect = UICollectionView(frame:CGRect(x: 0, y: 0, width: UIScreen.main().bounds.size.width, height: 200), collectionViewLayout: RoundImagesLayout())
        collect.delegate = self
        collect.dataSource = self
        return collect
    }()
    
    private lazy var page: UIPageControl = {
        let page = UIPageControl()
        page.frame = CGRect(x: 0, y: 150, width: UIScreen.main().bounds.size.width, height: 50)
        page.pageIndicatorTintColor = UIColor.gray()
        page.currentPageIndicatorTintColor = UIColor.white()
        return page
    }()

}


class RoundImagesLayout:UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        // 1.设置itemSize
        itemSize = collectionView!.frame.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = .horizontal
        
        // 2.设置collectionView的属性
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }
}


extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        collectionView.register(RoundCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! RoundCollectionViewCell
        cell.imageName = imagesArray[indexPath.item!]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (imagesArray.count)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let index = Int(scrollView.contentOffset.x / UIScreen.main().bounds.size.width)
        page.currentPage = index
        
        if scrollView.contentOffset.x < 0 {
            let  indexPath = NSIndexPath.init(item: self.imagesArray.count - 1, section: 0)
            self.collectionView.scrollToItem(at: indexPath as IndexPath, at: UICollectionViewScrollPosition.init(rawValue: 0), animated: false)
            return
        }
        
        if (scrollView.contentOffset.x) > CGFloat(imagesArray.count-1) * (UIScreen.main().bounds.size.width)  {
            let  indexPath = NSIndexPath.init(item: 0, section: 0)
            self.collectionView.scrollToItem(at: indexPath as IndexPath, at: UICollectionViewScrollPosition.init(rawValue: 0), animated: false)
            return
        }
    }
    
}


class RoundCollectionViewCell: UICollectionViewCell {

    var imageName: String? {
        didSet {
            roundImageView.image = UIImage(named: imageName!)
        }
    }
    lazy var roundImageView : UIImageView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        roundImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main().bounds.size.width, height: 200)
        contentView.addSubview(roundImageView)
    }

}




上一篇下一篇

猜你喜欢

热点阅读