轮播图

作者: 唯与耽车 | 来源:发表于2016-11-24 09:00 被阅读0次

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.
    }


}

相关文章

  • 无标题文章

    轮播图分为:传统轮播图、间歇轮播图、呼吸轮播图、无缝滚动轮播图等。它们各具特色,各有用处。 1.传统轮播图 第一步...

  • 轮播图

    轮播图分为:传统轮播图、间歇轮播图、呼吸轮播图、无缝滚动轮播图等。 1.传统轮播图 第一步,得到元素 第二步,设置...

  • 现金红包

    每日红包 轮播图 详情图 周末红包 轮播图 详情图 圣诞红包 轮播图 详情图

  • day7_作业

    轮播图1 轮播图2

  • [iOS]定制性强的广告轮播图--SCAdView

    @[无限轮播图] @[3D轮播图] @[广告轮播图] SCAdView Statement If my code ...

  • 普通奖品

    卡西欧小方块 轮播图 详情图 三只松鼠大礼包 轮播图 详情图 天猫精灵 轮播图 详情图 小米蓝牙无线耳机 轮播图 ...

  • 轮播图心得

    轮播图 写轮播图之前我们要认识到几个问题:一、什么是轮播图?二、怎么实现轮播效果?三、轮播图还有什么小功能可以实现...

  • 第五周学习内容

    焦点图轮播特效之原理、焦点图轮播样式之布局、焦点图轮播之箭头切换、焦点图轮播之无限滚动。 js简介、用法、输出。

  • 三种样式的轮播图

    一、100%比例轮播图 HTML代码 CSS样式 js代码 二、手动箭头轮播图 三、简易轮播图

  • 多轮播图的设计

    列表展示多个轮播图 多个轮播图同时显示 多个轮播图不定时进行轮播动画 对应呈现的关系:1个TableView、一个...

网友评论

      本文标题:轮播图

      本文链接:https://www.haomeiwen.com/subject/uhlbpttx.html