All digits not shown in timer countdown - ios

While coming to a view I call a function to load a timer like so...
var count = 10
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
}
and update function is given as..
#objc func update() {
while (count != 0) {
count -= 1
countdownLabel.text = "\(count)"
}
timer.invalidate()
}
But what happens is when I come to this view, straightaway the number 0 is shown as opposed to ideally displaying all numbers in the sequence 9,8,7,6,5,4,3,2,1,0
What am I doing wrong here..?

Swift 4:
var totalTime = 10
var countdownTimer: Timer!
#IBOutlet weak var timeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
}
This method call initializes the timer. It specifies the timeInterval (how often the a method will be called) and the selector (the method being called).
The interval is measured seconds so for it to perform like a standard clock we should set this argument to 1.
func startTimer() {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
// Stops the timer from ever firing again and requests its removal from its run loop.
func endTimer() {
countdownTimer.invalidate()
}
//updateTimer is the name of the method that will be called at each second. This method will update the label
#objc func updateTime() {
timeLabel.text = "\(totalTime)"
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
}

Related

Timer Updating Label 1 Second Behind 2nd Timer Counting Down

I'm having difficulties attempting to link two timers together. I'm trying to have a timer count down from a specified amount and to have a second timer constantly updating a label on a view controller. However, I end up having the timer that updates the label lagging exactly 1 second behind the first timer in the timer class. Here's what I have for the view controller: (note that this is a condensed version of my code)
class HomeViewController: UIViewController {
#IBOutlet var timeLabel: UILabel!
override func viewDidLoad() {
timeLabel.text = account.deedManager.globalTimer.timerStr
Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(UpdateTime), userInfo: nil, repeats: true)
}
#objc func UpdateTime() {
self.timeLabel.text = account.deedManager.globalTimer.timerStr
}
}
And here is the Timer class:
class TimerModel: NSObject, NSCoding {
var myTimer: Timer? = Timer()
var timerInterval: TimeInterval = 1.0
var timerEnd: TimeInterval = 0.0
var timerCount: TimeInterval = 86400.0 // 24 hours
var timerStr: String = "TIME"
func StartTimer(time: Double) {
timerCount = time
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(UpdateTime), userInfo: nil, repeats: true)
}
#objc func UpdateTime() {
self.timerStr = self.TimerDate(time: self.timerCount)
self.timerCount-=1
print(self.timerStr)
}
func TimerDate(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format: "%02i:%02i:%02i", hours, minutes, seconds)
}
}
I've tried to make the first timer a 0.05 interval so that it updates more rapidly than the timer class, but it lags behind exactly a second no matter what interval I put it at. I don't want to put the count down timer inside the view controller as I want the timer global for all view controllers. If you have any ideas, let me know.
First you need to switch the order around so that you are setting the text after you decrement the timerCount:
#objc func UpdateTime() {
self.timerCount-=1
self.timerStr = self.TimerDate(time: self.timerCount)
print(self.timerStr)
}
Then, you can delete the 0.05-second timer because as you said, that doesn't seem to work.
Try using the delegate pattern instead.
protocol TimerModelDelegate {
func timerTextDidChange(_ timer: TimerModel, text: String)
}
And then in TimerModel,
weak var delegate: TimerModelDelegate?
var timerStr: String = "TIME" {
didSet {
delegate?.timerTextDidChange(self, text: timerStr)
}
}
In HomeViewController, do this:
class HomeViewController: UIViewController, TimerModelDelegate {
#IBOutlet var timeLabel: UILabel!
override func viewDidLoad() {
timeLabel.text = account.deedManager.globalTimer.timerStr
account.deedManager.globalTimer.delegate = self
account.deedManager.globalTimer.StartTimer(time: 60)
}
// You don't need the UpdateTime method here
func timerTextDidChange(_ timer: TimerModel, text: String) {
timeLabel.text = text
}
}
You have this function:
#objc func UpdateTime() {
self.timerStr = self.TimerDate(time: self.timerCount)
self.timerCount-=1
print(self.timerStr)
}
Replace it with
#objc func UpdateTime() {
self.timerCount-=1
self.timerStr = self.TimerDate(time: self.timerCount)
print(self.timerStr)
}
Explanation:
You first have to change the value, then display it
Notice the first two lines are swapped. This should fix your issue.

If statement not working with timer

When 10 seconds hit the timer. I would like the background color to be change. Right now my code is not working. Nothing is being changed when the code hits 10 seconds. I also dont think the timer is working.
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if seconds == 10{self.view.backgroundColor = UIColor.red
}
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
}
}
Try this code:
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
if seconds == 10{
self.view.backgroundColor = UIColor.red
print("soda")
}
}
if statement should be written in #objc func clocker() not in viewDidAppear

NSTimer not working Swift

I've seen this a couple of times before, but it never occured to me what might be wrong.
Firstly, I want to create the effect of scrambling numbers like they do in those hacking scenes in movies. So, I made an NSTimer to make my delays such that every 0.2 seconds, the numbers change. Then, I made another timer to tell my first timer to
invalidate()
after two seconds. My code is as follows:
import UIKit
class MainPage: UIViewController {
#IBOutlet var genericDeviceName: UITextField!
#IBOutlet var hackButton: UIButton!
#IBOutlet var rightNumber: UILabel!
#IBOutlet var leftNumber: UILabel!
#IBOutlet var detectionText: UILabel!
#IBAction func deviceNameEnter(sender: AnyObject) {
detectionText.text = "Device detected: " + genericDeviceName.text!
if genericDeviceName.text == "" {
detectionText.text = "Error"
}
hackButton.alpha = 1
}
#IBAction func hackDevice(sender: AnyObject) {
var tries = 0
var timer = NSTimer()
var timerStop = NSTimer()
timer = NSTimer (timeInterval: 0.2, target: self, selector: "update", userInfo: nil, repeats: true)
timerStop = NSTimer (timeInterval: 2, target: self, selector: "endTimer", userInfo: nil, repeats: true)
let diceRoll = Int(arc4random_uniform(9) + 1)
let diceRollSecond = Int(arc4random_uniform(9) + 1)
UIView.animateWithDuration(0.25, animations:{
self.hackButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))})
func update() {leftNumber.text = String(diceRoll)
rightNumber.text = String(diceRoll)
print("it worked!")}
func endTimer() {
timer.invalidate()
detectionText.text = "Access Granted!"
timerStop.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
}
So... what went wrong? The last few times I tried using NSTimers, they didn't work either. Is my concept of an NSTimer wrong? Or is there an error in my code? There was no error message triggered, it was just that the timer did not trigger and the numbers did not change. Not even "it worked!" was printed to the logs. Please help by suggesting some code. Thank you in advance!
UPDATE
I've updated my code. Here it is:
import UIKit
class MainPage: UIViewController {
#IBOutlet var genericDeviceName: UITextField!
#IBOutlet var hackButton: UIButton!
#IBOutlet var rightNumber: UILabel!
#IBOutlet var leftNumber: UILabel!
#IBOutlet var detectionText: UILabel!
#IBAction func deviceNameEnter(sender: AnyObject) {
detectionText.text = "Device detected: " + genericDeviceName.text!
if genericDeviceName.text == "" {detectionText.text = "Error"}
hackButton.alpha = 1
}
let diceRoll = Int(arc4random_uniform(9) + 1)
let diceRollSecond = Int(arc4random_uniform(9) + 1)
func update(timer: NSTimer) {leftNumber.text = String(diceRoll)
rightNumber.text = String(diceRoll)
print("it worked!")}
func endTimer(timerStop: NSTimer) {
timer.invalidate()
detectionText.text = "Access Granted!"
timerStop.invalidate()}
#IBAction func hackDevice(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "update:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
var timerStop = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "endTimer:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timerStop, forMode: NSRunLoopCommonModes)
UIView.animateWithDuration(0.25, animations:{
self.hackButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))})
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Currently, it seems that the function "endTimer" does not work, due to the variable "timer" not being recognised. Please help. Thank you all so much for your time!
A few things: The selector for an NSTimer should end in a colon (e.g. "update:" or "endTimer:" And the function should take a single parameter: An NSTimer.
Second, the function that the timer calls must be a top-level function of the target. Your update method is a local function of your hackDevice, function, which won't work.
Third, you need to use scheduledTimerWithTimeInterval, as in ShahiM's answer:
var timer = NSTimer.scheduledTimerWithTimeInterval(
0.4,
target: self,
selector: "update:",
userInfo: nil,
repeats: true)
That code crashes if the function in your selector is a nested function because it's not visible to the timer.
Finally, it looks like you need to move the variables diceRoll and diceRollSecond out of your hackDevice function and make them instance variables of your class.
You should move your functions out of hackDevice. Nested functions like this are generally not used in Swift.
For example:
let diceRoll = Int(arc4random_uniform(9) + 1)
let diceRollSecond = Int(arc4random_uniform(9) + 1)
var timer = NSTimer()
#IBAction func hackDevice(sender: AnyObject) {
var tries = 0
var timer = NSTimer()
var timerStop = NSTimer()
timer = NSTimer (timeInterval: 0.2, target: self, selector: "update", userInfo: nil, repeats: true)
timerStop = NSTimer (timeInterval: 2, target: self, selector: "endTimer", userInfo: nil, repeats: true)
UIView.animateWithDuration(0.25, animations:{
self.hackButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))})
}
func update() {
leftNumber.text = String(diceRoll)
rightNumber.text = String(diceRoll)
print("it worked!")
}
func endTimer() {
timer.invalidate()
detectionText.text = "Access Granted!"
timerStop.invalidate()
}
Try using this :
var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
Also move your update and endTimer methods outside the hackDevice method.
Explanation :
From Apple docs :
Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)
.
Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.
So in your code, you only create the timer but it does not start running. You have to either call the addTimer(_ timer: NSTimer,forMode mode: String) to start the timer or you can simply use scheduledTimerWithTimeInterval to launch the timer right away.
You don't nest this kind of function, selector will not find them because they will be exposed after the method exit, after the function leave the last } there will be no a update and endTiemr
Your timer should look like this let timer = NSTimer(timeInterval: 0.2, target: self, selector: Selector("update:"), userInfo: nil, repeats: true)
and on the other side func update(timer: NSTimer) {
Also try adding the timer to the run loop after initialisation:
let timer = NSTimer(timeInterval: 0.2, target: self, selector: Selector("update:"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
In your update you redeclared the variable timer, this way you created a local variable which exists just in the method hackDevice: , remove the var before the timer = NSTimer.scheduledTimer...
Edit:
I rather edit this answer, because here i can add insert code snippet with proper newlines and indents:
class MainPage: UIViewController{
// Your IBOutlets
#IBOutlet var ...
var timer= NSTimter()
// Your methods
}

How can I make a function execute every second in swift?

I want to add a score to the top of my scene in the game I am working on. The score is going to based on how long you last, and will increase every second. Thanks for the help in advance!
import SpriteKit
class easyScene: SKScene {
let scrollBarEasyBottom = SKSpriteNode(imageNamed: "scrollBarEasyBottom")
let scrollBarEasyTop = SKSpriteNode(imageNamed: "scrollBarEasyTop")
let ball = SKSpriteNode(imageNamed: "ball")
var origSBEBpositionX = CGFloat(0)
var origSBETpositionX = CGFloat(0)
var maxSBEBX = CGFloat(0)
var SBEBSpeed = 5
var maxSBETX = CGFloat(0)
var SBETSpeed = 5
var score = 0
var timer: NSTimer?
var scoreText = SKLabelNode(fontNamed: "Kailasa")
override func didMoveToView(view: SKView) {
println("Easy Scene is the location")
self.backgroundColor = UIColor.blackColor()
self.scrollBarEasyBottom.position = CGPoint(x:0, y:270)
self.addChild(self.scrollBarEasyBottom)
self.scrollBarEasyBottom.yScale = 0.2
self.origSBEBpositionX = self.scrollBarEasyBottom.position.x
// end scrollBarEasyBottom
self.scrollBarEasyTop.position = CGPoint(x:20, y:400)
self.addChild(self.scrollBarEasyTop)
self.scrollBarEasyTop.yScale = 0.2
self.origSBETpositionX = self.scrollBarEasyTop.position.x
// end scrollBarEasyTop
self.ball.position = CGPoint(x:40, y:293)
self.addChild(self.ball)
self.ball.yScale = 0.17
self.ball.xScale = 0.17
// end ball
self.maxSBEBX = self.scrollBarEasyBottom.size.width - self.frame.size.width
self.maxSBEBX *= -1
self.maxSBETX = self.scrollBarEasyTop.size.width - self.frame.size.width
self.maxSBETX *= -1
//
self.scoreText.text = "0"
self.scoreText.fontSize = 60
self.scoreText.position = CGPoint(x: CGRectGetMidX(self.frame), y: 500)
self.scoreText.text = String(self.score)
self.addChild(self.scoreText)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("scoreIncrease") , userInfo: nil, repeats: true)
func scoreIncrease (){
score++
println(score)
}
}
override func update(currentTime: NSTimeInterval) {
if self.scrollBarEasyBottom.position.x <= maxSBEBX + 1200 {
self.scrollBarEasyBottom.position.x = self.origSBEBpositionX
}
if self.scrollBarEasyTop.position.x <= maxSBETX + 1200 {
self.scrollBarEasyTop.position.x = self.origSBETpositionX
}
scrollBarEasyBottom.position.x -= CGFloat(self.SBEBSpeed)
scrollBarEasyTop.position.x -= CGFloat(self.SBETSpeed)
// moving bars
var degreeRotation = CDouble(self.SBEBSpeed) * M_PI / 180
self.ball.zRotation -= CGFloat(degreeRotation)
//rotate ball
}
}
After running this code, I always get an
unrecognized selector sent to instance error
You can use one like this:
var timer = NSTimer()
override func viewDidLoad() {
scheduledTimerWithTimeInterval()
}
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounting"), userInfo: nil, repeats: true)
}
func updateCounting(){
NSLog("counting..")
}
Swift 3:
var timer = Timer()
override func viewDidLoad() { // Use for the app's interface
scheduledTimerWithTimeInterval()
}
override func didMove(to view: SKView) { // As part of a game
scheduledTimerWithTimeInterval()
}
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateCounting), userInfo: nil, repeats: true)
}
#objc func updateCounting(){
NSLog("counting..")
}
Swift 5:
Note: this solution is compatible with iOS 10.0+.
// If needing to check for iOS compatibility use
// if #available(iOS 10.0, *) {code}
var timer = Timer()
override func viewDidLoad() {
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
updateCounting()
})
}
func updateCounting(){
print("counting...")
}
You can then invalidate (stop) the timer using:
timer.invalidate()
There is something called NSTimer in swift which could solve your problem. I have given an example like how you can use it. Just customise it for your purpose.
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
target: self,
selector: Selector("yourMethodToCall"),
userInfo: nil,
repeats: true)
Add this line to the place where you need to call your function repeatedly.
The 1.0 refers to 1 second.
Change the selector to call yourMethodName
repeats is set to true to call that function every second.
Try this out and let me know if your are stuck somewhere. Thanks.
Swift 3
find this solution it worked for me
weak var timer: Timer?
var timerDispatchSourceTimer : DispatchSourceTimer?
func startTimer() {
if #available(iOS 10.0, *) {
timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { [weak self] _ in
// do something here
}
} else {
// Fallback on earlier versions
timerDispatchSourceTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
timerDispatchSourceTimer?.scheduleRepeating(deadline: .now(), interval: .seconds(60))
timerDispatchSourceTimer?.setEventHandler{
// do something here
}
timerDispatchSourceTimer?.resume()
}
}
func stopTimer() {
timer?.invalidate()
//timerDispatchSourceTimer?.suspend() // if you want to suspend timer
timerDispatchSourceTimer?.cancel()
}
// if appropriate, make sure to stop your timer in `deinit`
deinit {
stopTimer()
}
I prefer
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
// Do what you need to do repeatedly
}
}
To stop it:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if timer != nil {
timer?.invalidate()
timer = nil
}
}
Xcode 10.2 Swift 5:
override func viewDidLoad() {
super.viewDidLoad()
// ...
Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: Selector(("your #obcj func name")), userInfo: nil, repeats: true)
}
//Anywhere in the same view controller to stop the loop:
Timer.cancelPreviousPerformRequests(withTarget: your #obcj func name())
I don't think you need NSTimer for this.
Since you are using SpriteKit, I am going to suggest simplest solution in my opinion:
Declare a variable var prevScoreCalcTime:TimeInterval = 0
Inside of update func in your GameScene set it up like below:
override func update(_ currentTime: TimeInterval) {
if currentTime - prevScoreCalcTime > 1 {
prevScoreCalcTime = currentTime
// Any function you put here will execute every second
}
}
Good luck!
// For running a piece of code every second
///Runs every second, to cancel use: timer.invalidate()
#discardableResult public static func runThisEvery(
seconds: TimeInterval,
startAfterSeconds: TimeInterval,
handler: #escaping (CFRunLoopTimer?) -> Void) -> Timer {
let fireDate = startAfterSeconds + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, seconds, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes)
return timer!
}

Label not showing value of timer in Swift

I have a timer loop that executes a basic countdown and it prints the value to the console. I'm trying to have that value set to a text value of a label. Even though the Xcode console shows the correct countdown of the timer value, the label in the application still shows 0. Any ideas as to why this is happening? Here is the relevant code:
import UIKit
class GameViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
var timerCount = 7
var timerRunning = false
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
self.timerCount = 7
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
}
func Counting(){
timerCount = 7
do {
println(timerCount)
timerRunning = true
--timerCount
timerLabel.text = "\(timerCount)"
println(timerCount)
} while timerCount > 0
}
The method Counting() is wrong.
Every second you are launching the counting method and within that method you have a loop which updates the timerLabel.text, but the UI is not updated until the Counting() finishes...that's why is always showing 0. You need just to decrease the counting every second and update the label.
I think this is what you need:
func Counting(){
if timerCount == 0
{
timerCount = 7 // or self.timer.invalidate() in case you want to stop it
}
else
{
timerCount--;
timerLabel.text = "\(timerCount)"
println(timerCount)
}
}
Hope it helps

Resources