swift-轮播图

轮播图

2016年11.24

//轮播图

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate{

    //定义一个全局变量
    var scrollerView : UIScrollView! = nil
    var pageC : UIPageControl! = nil
    var timer : Timer! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let src = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 300))
        //全局
        scrollerView = src
        //设置代理
        src.delegate = self
        src .contentSize = CGSize(width: self.view.frame.size.width * 5, height: 300)
        //整页翻动
        src.isPagingEnabled = true
        //触壁反弹
        src.bounces = false
        self.view.addSubview(src)
        
        for index in 1...5 {
            let name = "\(index).jpg"
            let image = UIImage(named:name)
            let x = CGFloat(index - 1) * self.view.frame.size.width
            let imageView = UIImageView(frame: CGRect(x: x, y: 0, width: self.view.frame.size.width, height: 300))
            imageView.image = image
            src.addSubview(imageView)
            
        }
        //添加UIPageControl
        let page = UIPageControl(frame: CGRect(x: 100, y: 350, width: self.view.frame.size.width - 200, height: 30))
        //全局
        pageC = page
        
        page.numberOfPages = 4
        page.currentPage = 0
        page.addTarget(self, action: #selector(pageAction(page:)), for: .valueChanged)
        self.view.addSubview(page)
        
        //timer  自动播放
        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        self.timer.fire()
        
        
    }
    func timerAction() {
        //1,获取到当前的offset,   2,offset.x + width.  3, if 到第四个 跳回到第一个
        let offset = self.scrollerView.contentOffset
        let width = self.view.frame.size.width
        //让scrollerView进行滑动  offset.x+width:加宽度 走到下一张图片
        scrollerView.setContentOffset(CGPoint(x: offset.x + width,y:offset.y ), animated: true)
        //if 到第四个了,跳动到第一个
        if scrollerView.contentOffset.x >= width * 4 {
            let point = CGPoint(x: 0, y: 0)
            
        scrollerView.contentOffset = point
        }
        
    }
    
    //page是局部变量,必须写上page:UIPageControl
    func pageAction(page:UIPageControl) {
        let index = page.currentPage;
        let point = CGPoint(x: CGFloat(index) * self.view.frame.size.width, y: 0)
        //修改偏移量
        self.scrollerView.setContentOffset(point, animated: true)
        
    }
    //开始拖拽
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.timer.invalidate()
        self.timer = nil
    }
    
    //结束拖拽
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        
        //一秒之后开启
        let time =  DispatchTime.now() + Double(Int64(1*NSEC_PER_SEC))/Double(NSEC_PER_SEC)
        DispatchQueue.main.asyncAfter(deadline:time) {
            
           //循环绑定
            if let _ = self.timer {
                self.timer.fire()

            }
        }
    }
    
    //滑动会执行的方法
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取到偏移量,根据偏移量定位到第几个点
        let x = scrollerView.contentOffset.x
        let width = self.view.frame.size.width
        if (x >= width * 4) {
            self.pageC.currentPage = 0
            
        }else {
            self.pageC.currentPage = Int(x / width)
        }
        
        
        //这一行代码强制转换  同下。
        //self.pageC.currentPage = Int(x / width)
        
//        switch x {
//        case 0:
//           self.pageC.currentPage = 0
//        case 1 * width:
//            self.pageC.currentPage = 1
//        case 2 * width:
//            self.pageC.currentPage = 2
//        case 3 * width:
//            self.pageC.currentPage = 3
//            
//        default:
//            print("other")
//            
//        }
        
        
    }
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容