swift轮播图

import UIKit

let ImageViewCount = 3

class XGHomeHeadView: UIView {
    
    var scrollView: UIScrollView?
    var pageControl: UIPageControl?
    var timer: NSTimer?
    

    var banners: Array? {
        didSet {
            pageControl?.numberOfPages = (banners?.count)!
            pageControl?.currentPage = 0
            updateContent()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupScrollView()
        startTimer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupScrollView() {
        scrollView = UIScrollView(frame: self.bounds)
        scrollView?.showsVerticalScrollIndicator = false
        scrollView?.showsHorizontalScrollIndicator = false
        scrollView?.pagingEnabled = true
        scrollView?.delegate = self
        self.addSubview(scrollView!)
        
        for _ in 0..= pageControl?.numberOfPages {
                pageIndex = 0
            }
            
            imgView.tag = pageIndex
            let banner = banners![pageIndex] as! XGBanner
            let image = UIImage(named: banner.image!)
            
            imgView.image = image
        }
        scrollView?.contentOffset = CGPointMake(scrollView!.bounds.width, 0)
    }
    
    func startTimer() {
        if timer == nil {
            timer = NSTimer(timeInterval: 2.0, target: self, selector: #selector(nextImage), userInfo: nil, repeats: true)
            NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
        }
    }
    
    func nextImage() {
        scrollView?.setContentOffset(CGPointMake(2*(scrollView?.bounds.width)!, 0), animated: true)
    }
    
    func stopTimer() {
        timer?.invalidate()
        timer = nil
    }
    
    func imgViewDidClicked(ges:UITapGestureRecognizer) {
        let view = ges.view
        print(view?.tag)
    }
}

extension XGHomeHeadView: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        var page = 0
        var minDistance: CGFloat = CGFloat(MAXFLOAT)
        
        for imgView in scrollView.subviews {
            var distance: CGFloat = 0
            distance = (imgView.frame.origin.x-scrollView.contentOffset.x)
            distance = CGFloat(abs(Int(distance)))
            if distance < minDistance {
                minDistance = distance
                page = imgView.tag
            }
        }
        pageControl?.currentPage = page
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        updateContent()
    }
    
    func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
        updateContent()
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        stopTimer()
    }
    
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        startTimer()
    }
}

你可能感兴趣的:(swift轮播图)