图片轮播swift版本

有几个需要注意的地方,scallView的常用属性,代理的使用,定时器的使用,定时器不同模式的区别,UIPageControl的使用

//
//  ViewController.swift
//  图片轮播
//
//  Created by admin on 15/12/31.
//  Copyright © 2015年 jin. All rights reserved.
//

import UIKit
//遵循了UIScrollViewDelegate协议,作为scallView的代理
class ViewController: UIViewController,UIScrollViewDelegate{

    @IBOutlet weak var scallView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    var timer:NSTimer!
    override func viewDidLoad() {
        super.viewDidLoad()

        let w = scallView.frame.size.width
        let h = scallView.frame.size.height
        let colnum = 5
        for var i = 1;i <= colnum + 1;i++
        {
            var imageView:UIImageView = UIImageView()
            //多添加一个图片,让轮播效果更顺眼
            if i > colnum
            {
                imageView.image = UIImage(imageLiteral: "img_01")
            }
            else
            {
                imageView.image = UIImage(imageLiteral: "img_" + String(format: "%02i", i))
            }
            let x = CGFloat(i - 1) * w
            imageView.frame = CGRectMake(x, 0, w, h)
            scallView.addSubview(imageView)
        }

        //scallView可拖动区域的大小
        scallView.contentSize = CGSizeMake(CGFloat(colnum) * w, 0)
        scallView.showsHorizontalScrollIndicator = false//去掉横向滚动条
        scallView.pagingEnabled = true//打开分页
        //设置UIPageControl的点的个数
        pageControl.numberOfPages = column
        //设置scallView的代理
        scallView.delegate = self
        //设置活动分页的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        //设置活动分页之外分页的颜色
        pageControl.pageIndicatorTintColor = UIColor.blackColor()
        //添加定时器,自动切换图片
        self.addTimer()

    }
    //图片切换
    func imageChange()
    {
        var coordinates:CGPoint = CGPoint(x: 0,y: 0)
        //为了从最后一张切换到第一张的时候不那么突兀,这里需要处理
        if pageControl.currentPage == 0 && scallView.contentOffset.x > 0
        {
            coordinates = CGPoint(x: 0, y: scallView.contentOffset.y)
            self.scallView.contentOffset = coordinates
        }
        coordinates = CGPoint(x: CGFloat(pageControl.currentPage + 1) * scallView.frame.size.width, y: scallView.contentOffset.y)
        //动画效果
        UIView.animateWithDuration(1, animations: {
            self.scallView.contentOffset = coordinates
        })
    }
    //添加定时器的方法
    func addTimer()
    {
        //初始化定时器
        self.timer = NSTimer(timeInterval: 3, target: self, selector: "imageChange", userInfo: nil, repeats: true)
        //获得当前消息循环
        var runLoop = NSRunLoop.currentRunLoop()
        //把定时器以默认模式添加进当前消息循环,分为两种模式
//        runLoop.addTimer(self.timer, forMode: NSDefaultRunLoopMode)
        runLoop.addTimer(self.timer, forMode: NSRunLoopCommonModes)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //scrollView代理方法,滚动时
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获得当前页数
        let currentPage = Int((scallView.contentOffset.x + (scallView.frame.size.width * 0.5)) / scallView.frame.size.width)
        //因为多加了一个图片,所以这里需要处理下
        if currentPage >= pageControl.numberOfPages
        {
            pageControl.currentPage = 0
        }
        else
        {
            pageControl.currentPage = currentPage
        }
    }
    //scallView代理方法,开始拖拽
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        self.timer.invalidate()
    }
    //scallView代理方法,结束拖拽
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        //创建定时器并加入当前消息循环,默认模式
//        self.timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "imageChange", userInfo: nil, repeats: true)
        self.addTimer()
    }

}

标签: swift, ios控件, 定时器

添加新评论