码迷,mamicode.com
首页 > 编程语言 > 详细

论 Swift 开发入门 : 进度条(UIProgressView)

时间:2015-04-01 09:31:29      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:ios   swift   进度条   uiprogressview   

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44802019
------------------------------------------------------------------------------------------

概述

技术分享
------------------------------------------------------------------------------------------

代码示例

import UIKit

class ViewController: UIViewController {
    
    var button: UIButton!
    var progressView: UIProgressView!
    
    var timer: NSTimer!
    var remainTime = 100
    
    override func viewDidLoad() {

        // 初始化按钮,开始倒计时
        button = UIButton.buttonWithType(.System) as UIButton
        button.frame = CGRectMake(self.view.frame.width/2 - 50, 50, 100, 50)
        button.setTitle("开始", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 初始化 progressView
        progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)
        progressView.frame = CGRectMake(self.view.frame.width/2 - 50, 200, 100, 100)
        // 设置初始值
        progressView.progress = 1.0
        // 设置进度条颜色
        progressView.progressTintColor = UIColor.blueColor()
        // 设置进度轨迹颜色
        progressView.trackTintColor = UIColor.greenColor()
        // 扩展:可以通过 progressImage、trackImage 属性自定义出个性进度条
        
        self.view.addSubview(button)
        self.view.addSubview(progressView)
    }
    
    /// 响应按钮点击事件,开始倒计时
    func buttonAction() {
        button.enabled = false
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats:true)
        timer.fire()
    }
    
    // 每秒定时触发
    func timerAction() {
        if(remainTime < 0){
            //倒计时结束
            timer.invalidate()
        } else {
            println("\(remainTime)")
            remainTime = remainTime - 1
            let progressValue = Float(remainTime)/100
            progressView.setProgress(progressValue, animated:true)
        }
    }
    
}
------------------------------------------------------------------------------------------

结语

GitHub 上项目地址:UIProgressViewSample

文章最后更新时间:2015年4月1日09:08:34。参考资料如下:

UIProgressView Class Reference

UIKit User Interface Catalog: Progress Views

论 Swift 开发入门 : 进度条(UIProgressView)

标签:ios   swift   进度条   uiprogressview   

原文地址:http://blog.csdn.net/jinnchang/article/details/44802019

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!