Swift 图片轮播

需要引用第三方:SDWebImage

import UIKit

class ScrollerView: UIView, UIScrollViewDelegate {
    
    var delegate: LYHScrollerViewDelegate!
    var scrollView: UIScrollView!
    var page: UIPageControl!
    var timer: NSTimer!
    
    var array: [String]? {
        
        willSet(newValue) {
            
            self.array = newValue
            
        }
        
        didSet {
            
            self.scrollView = UIScrollView(frame: self.frame)
            scrollView.contentSize = CGSizeMake(self.frame.size.width * CGFloat((array?.count)! + 2), 0)
            scrollView.contentOffset = CGPointMake(self.frame.size.width, 0)
            scrollView.pagingEnabled = true
            scrollView.bounces = false
            scrollView.delegate = self;
            scrollView.showsHorizontalScrollIndicator = false
            let tap = UITapGestureRecognizer(target: self, action: "tap")
            scrollView.addGestureRecognizer(tap)
            self .addSubview(self.scrollView)
            
            let firstImageView = UIImageView(frame: CGRectMake(0, 0, self.frame.size.width, 200))
            
            firstImageView.sd_setImageWithURL(NSURL.init(string: (array?.last)!), placeholderImage: UIImage.init(named: ""))
            self.scrollView.addSubview(firstImageView)
            
            let lastImageView = UIImageView(frame: CGRectMake(self.frame.size.width * CGFloat((array?.count)! + 1), 0, self.frame.size.width, 200))
            lastImageView.sd_setImageWithURL(NSURL.init(string: (array?.first)!), placeholderImage: UIImage.init(named: ""))
            self.scrollView.addSubview(lastImageView)
            
            for var i = 0; i < array?.count; ++i {
                
                let imageView = UIImageView.init(frame: CGRectMake(self.frame.size.width * CGFloat(i + 1), 0, self.frame.size.width, 200))
                imageView.sd_setImageWithURL(NSURL.init(string: self.array![i]), placeholderImage: UIImage.init(named: ""))
                
                self.scrollView.addSubview(imageView)
            }
        
            self.page = UIPageControl()
            page.frame = CGRectMake(0, 0, 200, 20)
            page.center = CGPointMake(self.frame.size.width / 2, 185)
            page.numberOfPages = (array?.count)!
            page.currentPageIndicatorTintColor = UIColor.orangeColor()
            page.pageIndicatorTintColor = UIColor.whiteColor()
            page.addTarget(self, action: "handlePage:", forControlEvents: .ValueChanged)
            self .addSubview(page)
            
            self.addTimer()
        }
        
    }
    
    // 定时器方法实现
    func time() {
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.frame.size.width, 0)
            
            
            }) { (Bool) -> Void in
                
                self.scrollViewDidEndDecelerating(self.scrollView)
                
        }
        
    }
    
    // scrollViewDelegate 
    //减速停止了时执行,手触摸时执行执行
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        if self.scrollView == scrollView {
           
            if self.scrollView.contentOffset.x == 0 {
                
                self.scrollView.contentOffset = CGPointMake(self.frame.size.width * CGFloat((array?.count)!), 0)
            }
            
            if self.scrollView.contentOffset.x == self.frame.size.width * CGFloat(((array?.count)! + 1)) {
                
                self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0)
            }
            
            self.page.currentPage = Int(self.scrollView.contentOffset.x / self.frame.size.width)  - 1
        }
        
        
    }
    
    // 开始拖拽
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        //关闭定时器
        self.removeTimer()
    }
    // 完成拖拽
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    
        //添加定时器
        
      self.addTimer()
    }
    
    // 添加定时器方法
    func addTimer (){
        
        self.timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "time", userInfo: nil, repeats: true)
        NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
    }
    // 移除定时器方法
    
    func removeTimer (){
        
        self.timer.invalidate()
        self.timer = nil
    }
    // 重写初始化方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // scrollView添加的手势的点击方法
    func tap () {
        
        if (delegate != nil) {
            
            delegate.selectt(Int(self.scrollView.contentOffset.x - self.frame.size.width) / Int(self.frame.size.width))
        }
        
    }

}

// 声明协议
@objc protocol LYHScrollerViewDelegate: NSObjectProtocol {
    //回调方法
    func selectt(index:Int)
    
}

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