Wednesday, 8 March 2017

UIProgressView : Dynamic





//
//  ViewController.swift
//  TestApp2
//
//  Created by Divakar Singh on 3/7/17.
//  Copyright (c) 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
   
     var progressView: UIProgressView!
     var progressLabel: UILabel!
    var btn1 : UIButton!
    
    var current: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create Progress View Control
        progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
        progressView.center = self.view.center
        view.addSubview(progressView!)
        
        // Add Label
        progressLabel = UILabel()
        let frame = CGRectMake(25, 100, 100, 50)
        progressLabel.frame = frame
        progressLabel.text = "0"
        progressLabel.backgroundColor = UIColor.orangeColor()
        view.addSubview(progressLabel)
        
        // Add Button
        btn1 = UIButton()
        let frame1 = CGRectMake(25, 20, 100, 50)
        btn1.setTitle("Click me", forState: UIControlState.Normal)
        btn1.frame = frame1
        btn1.backgroundColor = UIColor.orangeColor()
        view.addSubview(btn1!)
        
        btn1.addTarget(self, action: "actionTriggered:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func actionTriggered(sender: AnyObject) {
        
        // Get current values.
        let i = current
        let max = 10
        
        // If we still have progress to make.
        if i <= max {
            // Compute ratio of 0 to 1 for progress.
            let ratio = Float(i) / Float(max)
            // Set progress.
            progressView.progress = Float(ratio)
            // Write message.
            progressLabel.text = "Processing \(i) of \(max)..."
            current++
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}

No comments:

Post a Comment