In this example I start a timer as soon as I enter the second view controller after clicking the "START MY RANDOM WORKOUT" button. Here is the code for that second view:
(Swift 3)
import UIKit class ExerciseVc: UIViewController { @IBOutlet weak var timerLabel: UILabel! @IBOutlet weak var icon: UIImageView! var timer: Timer! var timeLeft = 60 override func viewDidLoad() { super.viewDidLoad() // Call setTimeLeft every 1 second timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.setTimeLeft), userInfo: nil, repeats: true) icon.image = #imageLiteral(resourceName: "situp") } func setTimeLeft() { timeLeft -= 1 // Subtract one second if timeLeft == 0 { stop() } timerLabel.alpha = 1 timerLabel.transform = CGAffineTransform(scaleX: 1.4, y: 1.4) UIView.animate(withDuration: 1, animations: { self.timerLabel.text = "\(self.timeLeft)" self.timerLabel.alpha = 0.3 self.timerLabel.transform = .identity }) } @IBAction func stop(_ sender: UIButton) { stop() } // Show a white status bar instead of the default black one override var preferredStatusBarStyle: UIStatusBarStyle { get { return UIStatusBarStyle.lightContent } } func stop() { timer.invalidate() // Stop the timer dismiss(animated: true, completion: nil) } }
(Swift 3)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.