轮播图

UIScrollView

定义全局变量

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {
## 定义全局变量(scrollerView,pageC,timer)
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.width, height: 300))
      src.contentSize = CGSize(width: self.view.frame.size.width * 5, height: 300)
        
        scrollerView = src
        //设置代理
        src.delegate = self
        src.isPagingEnabled = true
        src.bounces = false
        self.view.addSubview(src)
        for index in 1...5{
            let name = "\(index).jpeg"
            let image = UIImage(named:name)
            let x = CGFloat(index-1) * self.view.frame.size.width
            let imageV = UIImageView(frame: CGRect(x: x, y: 0, width: self.view.frame.size.width, height: 300))
            imageV.image = image
            src.addSubview(imageV)
}

//添加UIPageControl

    let page = UIPageControl()
        page.frame = CGRect(x: 100, y: 350, width: self.view.frame.size.width - 200, height: 30)
        
        self.pageC = page
        page.numberOfPages = 4
        page.currentPage = 0
        page.addTarget(self, action: #selector(pageAction(page:)), for: .valueChanged)
        page.pageIndicatorTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        page.currentPageIndicatorTintColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
        self.view.addSubview(page)

//计时器
//timer


self.timer=Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        self.timer.fire() 
    }

//开始拖拽

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.timer.invalidate()
        self.timer = nil
    }
    //结束拖拽
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.timer=Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        self.timer.fire()
        //一秒之后再执行
       let time =  DispatchTime.now() + 1.0
        DispatchQueue.main.asyncAfter(deadline: time){
            if let _ = self.timer{
                self.timer.fire()
            }
         }
        }

//计时器的方法

func timerAction(){
        //获取当前offset
        let offset = self.scrollerView.contentOffset
        let width = self.view.frame.size.width
        //让scrollerView进行滑动
        scrollerView.setContentOffset(CGPoint(x: offset.x + width, y: offset.y), animated: true)
        //offset.x + width
        //if 到第四个了,跳动到第一个
        if scrollerView.contentOffset.x >= width * 4{
            let point = CGPoint(x: 0, y: 0)
            scrollerView.contentOffset = point 
        }
    }
    
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 scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取到偏移量
        let x = scrollerView.contentOffset.x
        let width = self.view.frame.size.width
        self.pageC.currentPage = Int(x / width)  //取代switch case中的一大串代码
        
        if (x >= width * 4) {
            self.pageC.currentPage = 0
        }else{
            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")
//        }
       
    }
override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

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