Thursday, February 21, 2019

Make countdown clock Swift

We want to make a countdown clock to show time remain to user when they playing game.
Declare a time value to a variable.
var count=100
Create a function like this
func update(){
        count = count - 1
        var minute=count/60
        minute=Int(minute)
        let second=count%60
        if(count<61){
         label.text=String(count)+" Seconds"
        }
        else{
       label.text=String(minute)+" Minute "+String(second)+" Seconds"
        }
    if count==0{
       let vc = second()
        self.presentViewController(vc, animated: true, completion: nil)
    }
    }
We divide seconds to 60 to find minute number, if seconds smaller tan 61, show only seconds remain. When time out, we open new screen.
Copy in to viewDidLoad.
let time=NSTimer.scheduledTimerWithTimeInterval(1.0, target:self,  selector: "update",userInfo: nil, repeats: true)
Run to see result.


No comments:

Post a Comment