Swift - 利用UIScrollView实现轮播图

看过网上很多轮播图的Demo,都是通过UITableView 或者 UICollectionView来实现的, 心血来潮用UIScrollView来实现一下,没做代码优化,只是个Demo。欢迎拍砖

轮播图

//
//  TimerScrollView.swift
//  时光网Swift
//
//  Created by 亓大志 on 16/4/20.
//  Copyright © 2016年 亓大志. All rights reserved.
//

import UIKit
protocol TimerScrollViewDelegate {
    func scrollToIndexOfPage(index:Int)
    func tapActionIndexOfPage(index:Int)
}

class TimerScrollView: UIView,UIScrollViewDelegate {
    var count : Int = 0
    var timer : NSTimer!
    var timerDelegate:TimerScrollViewDelegate?
    var scrollView : UIScrollView?
    var pageCT : UIPageControl?
    
    /*
    params:
        array : 图片URL集合
        contentOffSetIndex : 偏移位置
    */
    func configScrollView(array:NSArray,contentOffsetIndex:Int){
        count = array.count
        self.clipsToBounds = false
        self.backgroundColor = UIColor.whiteColor()
        scrollView = UIScrollView(frame: self.bounds)
        scrollView!.delegate = self
        scrollView!.contentSize = CGSizeMake(CGFloat(array.count+2) * self.frame.size.width, self.frame.size.height)
        scrollView!.pagingEnabled = true
        scrollView!.showsHorizontalScrollIndicator = false
        scrollView!.showsVerticalScrollIndicator = false
        self.addSubview(scrollView!)
        let tap = UITapGestureRecognizer(target: self, action: Selector("tapAction:"))
        scrollView?.addGestureRecognizer(tap)
        createImageViews(array,contentOffsetIndex:contentOffsetIndex)
        
        configPageControll(contentOffsetIndex)
        
        timerBegin()
    }
    
    func createImageViews(array:NSArray,contentOffsetIndex:Int){
        for i in 0 ..< array.count+2{
            var url : String!
            if i == 0 {
                url = array[array.count-1] as! String
            }else if i == array.count + 1 {
                url = array[0] as! String
            }else {
                url = array[i-1] as! String
            }
            
            
            let URL : NSURL = NSURL(string: url)!
            let imageView = UIImageView(frame: CGRectMake(CGFloat(i)*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height))
            //图片的加载需要依赖于框架,
            imageView.sd_setImageWithURL(URL)
            scrollView!.addSubview(imageView)
        }
        
        scrollView!.contentOffset = CGPointMake(CGFloat(contentOffsetIndex+1)*self.frame.size.width, 0)
    }
    
    func configPageControll(currentPage:Int) {
        pageCT = UIPageControl(frame: CGRectMake(0,self.frame.size.height-30.0,self.frame.size.width,15))
        pageCT!.pageIndicatorTintColor = UIColor.grayColor()
        pageCT!.currentPageIndicatorTintColor = UIColor.whiteColor()
        pageCT!.numberOfPages = count
        pageCT!.currentPage = currentPage
        self.addSubview(pageCT!)
    }
    
    //开启
    func timerBegin() {
        if timer != nil {
            timer.invalidate()
            timer = nil
        }
        
        timer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("timerRun:"), userInfo: nil, repeats: true)
        NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
    }
    
    //暂停
    func timerSuspend() {
        if timer != nil {
            timer.invalidate()
            timer = nil
        }
    }
    
    func timerRun(runTimer:NSTimer) {
        let offsetX = scrollView!.contentOffset.x
        UIView.animateWithDuration(0.4, animations: { () -> Void in
                self.scrollView!.contentOffset = CGPointMake(offsetX+self.frame.size.width, 0)
            }) { (boolValue:Bool) -> Void in
                self.resetContentOffset()
        }
        
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        timerSuspend()
    }
    
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        timerBegin()
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        resetContentOffset()
    }
    
    func resetContentOffset(){
        if scrollView!.contentOffset.x < self.frame.size.width {
            scrollView!.contentOffset = CGPointMake(self.frame.size.width*CGFloat(count), 0)
        }
        
        if scrollView!.contentOffset.x > self.frame.size.width * CGFloat(count) {
            scrollView!.contentOffset = CGPointMake(self.frame.size.width, 0)
        }
        
        
        let vc = self.timerDelegate as! UIViewController
        if vc.respondsToSelector(Selector("scrollToIndexOfPage:")) {
            let index = Int((scrollView!.contentOffset.x - self.frame.size.width) / self.frame.size.width)
            pageCT?.currentPage = index
            timerDelegate?.scrollToIndexOfPage(index)
        }
        
        
    }
    
    
    func tapAction(tap:UITapGestureRecognizer){
        let vc = self.timerDelegate as! UIViewController
        if vc.respondsToSelector(Selector("scrollToIndexOfPage:")) {
            let index = Int((scrollView!.contentOffset.x - self.frame.size.width) / self.frame.size.width)
            timerDelegate?.tapActionIndexOfPage(index)
        }
        
    }
    
    //解决 当UIView上面的UIButton超出UIView的范围时,UIButton点击范围问题
    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        let result = super.hitTest(point, withEvent: event)
        for sub in self.subviews {
            if sub is UIButton {
                let button = sub as! UIButton
                let buttonPoint = button.convertPoint(point, fromView: self)
                if button.pointInside(buttonPoint, withEvent: event) {
                    return button
                }
            }
        }
        return result
    }
}

GitHub地址:TimerForScrollView

第一篇文章,先塞点代码,后期维护一下,_

你可能感兴趣的:(Swift - 利用UIScrollView实现轮播图)