There are 2 Arrays. First one contains Strings that i want to show on UILabel. Second one contains their waiting durations on UILabel.
let items = ["stone","spoon","brush","ball","car"]
let durations = [3,4,1,3,2]
And two variables for specifying which one is on the go.
var currentItem = 0
var currentDuration = 0
This one is the timer system:
var timer = NSTimer()
var seconds = 0
func addSeconds () {seconds++}
func setup () {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "addSeconds", userInfo: nil, repeats: true)
}
Finally, that's the loop. Answer of: Which Array item stays how many seconds on the UILabel question.
func flow () {
while seconds <= durations[currentDuration] {
myScreen.text = items[currentItem]
if seconds == durations[currentDuration]{
seconds == 0
currentItem++
currentDuration++
}
}
Label and Button:
#IBOutlet weak var myScreen: UILabel!
#IBAction func startButton(sender: UIButton) {
setup()
}
}
If i change this:
func addSeconds () {seconds++}
To that:
func addSeconds () {seconds++ ; flow () }
For setting the loop, nothing happens. Even NSTimer, it stops at 1st second.
Because your flow method has a while loop that never exits and blocks the main thread, so the timer can never fire.
Don't use a while loop. Us the method triggered by the timer to update the UI.
So:
func addSeconds () {
seconds++
myScreen.text = items[currentItem]
if seconds == durations[currentDuration] {
seconds == 0
currentItem++
currentDuration++
}
}
Related
so I have tried 2 different ways to display the timer count down on the screen.
the code will print to the console but not to the UITextView (in both loop cases) in the repeat the UITextView ends with a 0 and that is the only thing it displays other than original txt "time count".... in the case where the commented loop is implemented the UITextView only displays the 1 (end of count down)... why is it printing to the console though these commands are in the same brackets as UITextView and they repeat
the image is after running code and clicking Soft (this is spin off of app brewery egg timer)
//
// ViewController.swift
// EggTimer
//
// Created by Angela Yu on 08/07/2019.
// Copyright © 2019 The App Brewery. All rights reserved.
//
import UIKit
let softTime = 5
let medTime = 7
let hardTime = 12
class ViewController: UIViewController {
#IBOutlet weak var timerTextView: UITextView!
#IBAction func eggHardnessButton(_ sender: UIButton) {
let hardness = sender.currentTitle
func timeCountDownSoft() {
var time = 3
repeat {
time = time - 1 //repeats
timerTextView.selectAll("") // does not repeat
timerTextView.insertText("") // does not repeat
let timeWord = String(time) // repeats
timerTextView.insertText(timeWord)//does not repeat
print(time) //repeats
sleep(1) //repeats
} while time >= 1
/*for timer in stride(from: 2, to: 0 , by: -1){
let time = String(timer)
timerTextView.selectAll("")
timerTextView.insertText("")
timerTextView.insertText(time)
print(time)
sleep(1)
}*/
}
func timeCountDownMed() {
for timer in stride(from: 420, to: 0 , by: -1) {
print(timer)
sleep(1)
}
}
func timeCountDownHard() {
for timer in stride(from: 720, to: 0 , by: -1) {
print(timer)
sleep(1)
}
}
if hardness == "Soft" {
print(softTime) // does not repeat
timeCountDownSoft() // does not repeat
} else if hardness == "Medium" {
print(medTime)
timeCountDownMed()
} else {
print(hardTime)
timeCountDownHard()
}
}
}
You never (well, almost never) want to use sleep().
The reason your text is not updating is because you are running closed-loops that never allow UIKit to update the view.
What you want to do instead is to use a repeating Timer with a one-second interval. Each time the timer fires, decrement your counter and update the UI. When the counter reaches Zero, stop the timer.
Here's a simple example:
import UIKit
let softTime = 5
let medTime = 7
let hardTime = 12
class ViewController: UIViewController {
#IBOutlet weak var timerTextView: UITextView!
var theTimer: Timer?
#IBAction func eggHardnessButton(_ sender: UIButton) {
let hardness = sender.currentTitle
var numSeconds = 0
// set number of seconds based on button title
switch hardness {
case "Soft":
numSeconds = softTime * 60
case "Medium":
numSeconds = medTime * 60
case "Hard":
numSeconds = hardTime * 60
default:
// some other button called this func, so just return
return()
}
// stop current timer if it's running
if let t = theTimer {
if t.isValid {
t.invalidate()
}
}
// update text field with selected time
let timeWord = String(numSeconds)
self.timerTextView.text = timeWord
// start a timer with 1-second interval
theTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
numSeconds -= 1
let timeWord = String(numSeconds)
self.timerTextView.text = timeWord
// if we've reached Zero seconds remaining
if numSeconds == 0 {
// stop the timer
timer.invalidate()
}
}
}
}
Several points:
When your app changes the UI, those changes do not actually get
rendered to the screen until your code returns and visits the app's
event loop. Thus, with your repeat loop, nothing will show up on in
the UI until the loop completes
Do not use sleep() on the main thread. That blocks everything, and
your app comes to a screeching halt.
If you want to update the UI once a second, set up a repeating timer
that fires once a second and do your UI updates in the timer code.
So I'm doing a simple game on swift 5 and I basically have a 3..2..1.. go timer to start the game and then a 3..2..1..stop timer to stop the game. And finally a function that displays the score. I need a way for each function call to wait for the timer to be done before the next one begins, any suggestions? Here's my code so far. (Also if you have any other suggestions on the app let me know as well, the end goal is to register how many taps of a button you can do in 3 seconds)
var seconds = 3 //Starting seconds
var countDownTimer = Timer()
var gameTimer = Timer()
var numberOfTaps = 0
override func viewDidLoad() {
super.viewDidLoad()
self.startCountdown(seconds: seconds)
self.gameCountdown(seconds: seconds)
self.displayFinalScore()
}
func startCountdown(seconds: Int) {
countDownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
self?.seconds -= 1
if self?.seconds == 0 {
self?.countdownLabel.text = "Go!"
timer.invalidate()
} else if let seconds = self?.seconds {
self?.countdownLabel.text = "\(seconds)"
}
}
}
func gameCountdown(seconds: Int) {
gameTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
self?.seconds -= 1
if self?.seconds == 0 {
self?.countdownLabel.text = "Stop!"
timer.invalidate()
} else if let seconds = self?.seconds {
self?.countdownLabel.text = "\(seconds)"
}
}
}
deinit {
// ViewController going away. Kill the timer.
countDownTimer.invalidate()
}
#IBOutlet weak var countdownLabel: UILabel!
#IBAction func tapMeButtonPressed(_ sender: UIButton) {
if gameTimer.isValid {
numberOfTaps += 1
}
}
func displayFinalScore() {
if !gameTimer.isValid && !countDownTimer.isValid {
countdownLabel.text = "\(numberOfTaps)"
}
}
You should think about the states your game could be in. It could be -
setup - Establish the game
starting - The first three seconds
running - After the first three seconds but before the end
ending - In the final three seconds
ended - Time is up.
Each time your timer ticks you need to consider what action do you need to take and what state do you need to move to. You haven't said how long you want the game to last, but let's say it is 30 seconds.
When a new game is started, you are in the setup state; The button is disabled (ie. it doesn't react to taps) and you set the score to 0. You move to the starting state.
In the starting you show the countdown. After three seconds you enable the button and move into the running state.
Once you reach 27 seconds, you move into the ending state and show the end count down
Finally time is up and you move into the ended state, disable the button and show the score.
You could code it something like this
enum GameState {
case setup
case starting
case running
case ending
case ended
}
class ViewController: UIViewController {
#IBOutlet weak var startButton: UIButton!
#IBOutlet weak var tapButton: UIButton!
#IBOutlet weak var countdownLabel: UILabel!
var gameState = GameState.ended
var gameTimer:Timer?
var numberOfTaps = 0
var gameStartTime = Date.distantPast
let GAMEDURATION: TimeInterval = 30
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func startButtonTapped(_ sender: UIButton) {
self.startGame()
}
#IBAction func tapMeButtonPressed(_ sender: UIButton) {
self.numberOfTaps += 1
}
func startGame() {
self.gameState = .setup
self.gameTimer = Timer.scheduledTimer(withTimeInterval:0.1, repeats: true) { timer in
let elapsedTime = -self.gameStartTime.timeIntervalSinceNow
let timeRemaining = self.GAMEDURATION-elapsedTime
switch self.gameState {
case .setup:
self.gameStartTime = Date()
self.tapButton.isEnabled = false
self.startButton.isEnabled = false
self.numberOfTaps = 0
self.gameState = .starting
case .starting:
if elapsedTime > 2.5 {
self.gameState = .running
self.tapButton.isEnabled = true
self.countdownLabel.text = "Go!"
} else {
let countdown = Int(3-round(elapsedTime))
self.countdownLabel.text = "\(countdown)"
}
case .running:
if timeRemaining < 4 {
self.gameState = .ending
}
case .ending:
let countdown = Int(timeRemaining)
self.countdownLabel.text = "\(countdown)"
if timeRemaining < 1 {
self.countdownLabel.text = "Stop"
self.gameState = .ended
self.tapButton.isEnabled = false
}
case .ended:
if timeRemaining <= 0 {
self.countdownLabel.text = "You tapped the button \(self.numberOfTaps) times"
self.startButton.isEnabled = true
self.gameTimer?.invalidate()
self.gameTimer = nil
}
}
}
}
}
The approach shouldn't be that the function calls wait for the timer to get finished, rather the timers should call the functions when they finish.
So, you need to move below function calls out of viewDidLoad and put them inside the Timer blocks.
self.gameCountdown(seconds: seconds)
self.displayFinalScore()
I.e. the function call self.gameCountdown(seconds: seconds) will go inside the timer block started in startCountdown. In that, when you are invalidating the timer when the seconds become 0, you call gameCountdown.
Similarly, in the timer started in gameCountdown, you call the self.displayFinalScore when the seconds become 0.
Few other suggestions. You should avoid checking properties in tapMeButtonPressed.
You should rather disable and enable the tap me button instead. I.e. enable it when you start the gameCountdown and disable it when it ends.
Similarly, you shouldn't need to check the state of the timers in displayFinalScore. It should just do one thing i.e. display the final score.
Will save you a lot of headaches later :). My 2 cents.
I just wonder what is the best implementation of memory optimized versatile multi Timers in swift.
The timers which are concurrent and have weak reference with Dispatch?
I've tried to implement two timers in one view controller and I got an error.
one of my timer was like this:
func startOnPlayingTimer() {
let queue = DispatchQueue(label: "com.app.timer")
onPlayTimer = DispatchSource.makeTimerSource(queue: queue)
onPlayTimer!.scheduleRepeating(deadline: .now(), interval: .seconds(4))
onPlayTimer!.setEventHandler { [weak self] in
print("onPlayTimer has triggered")
}
onPlayTimer!.resume()
}
another one was:
carouselTimer = Timer.scheduledTimer(timeInterval: 3, target: self,selector: #selector(scrollCarousel), userInfo: nil, repeats: true)
I dont think there is need for multiple timer for any application.
If from before hand you know which methods you are going to fire, Keep boolean for each method you need to fire and also an Int to save the occurrence of the method. You can invoke the timer once, with a method that checks for the required boolean and its respective method.
A pseudo code referencing the above logic is below :
class ViewController: UIViewController {
var myTimer : Timer!
var methodOneBool : Bool!
var methodTwoBool : Bool!
var mainTimerOn : Bool!
var mainTimerLoop : Int!
var methodOneInvocation : Int!
var methodTwoInvocation : Int!
override func viewDidLoad() {
super.viewDidLoad()
configure()
}
func configure(){
methodOneBool = false
methodTwoBool = false
methodOneInvocation = 5 // every 5 seconds
methodTwoInvocation = 3 //every 3 seconds
mainTimerOn = true // for disable and enable timer
mainTimerLoop = 0 // count for timer main
}
func invokeTimer(){
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(checkTimerMethod), userInfo: nil, repeats: true)
}
func checkTimerMethod(){
if(mainTimerOn){
if(mainTimerLoop % methodOneInvocation == 0 && methodOneBool){
// perform first method
// will only get inside this when
// methodOneBool = true and every methodOneInvocation seconds
}
if(mainTimerLoop % methodTwoInvocation == 0 && methodTwoBool){
// perform second method
// will only get inside this when
// methodTwoBool = true and every methodTwoInvocation seconds
}
mainTimerLoop = mainTimerLoop + 1
}
}
}
I hope this clears up the problem, also if I didnt understand your requirement please comment below, so that I can edit the answer accordingly
I have a coding 'issue'.
I have a label, which text I want to change dynamically every 2 seconds.
I've done the following:
// WELCOME STRING ARRAY
let welcomeContainer:[String] = ["Welcome","Benvenuti","Bienvenue","Willkommen","üdvözlet","Dobrodošli","добро пожаловать","Witajcie","Bienvenido","Ласкаво просимо","Vitajte","欢迎你来"]
and then, rather than using a timerwithinterval (which seemed to be too much for this simple task), I tried with the delay method in my function inside for loop:
func welcomeLabelChange() {
for i in 0..<welcomeContainer.count {
welcomeLabel.text = welcomeContainer[i]
delay(delay: 2.0, closure: {})
}
Unfortunately it's entirely skipping the delay... the for loop is executed instantly and just the last text in the array is displayed.
What am I doing wrong?
I found this OBJ-C answer, but it's suggesting an (old) NSTimer implementation.
You can also use this function to delay something
//MARK: Delay func
func delay(_ delay:Double, closure:#escaping ()->()) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
and usage is :
delay(2) //Here you put time you want to delay
{
//your delayed code
}
Hope it will help you.
define those variables
var i = 0
let timer : Timer?
Place this timer in your view did load or wherever you want to start the label change
timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector:#selector(YourViewController.changeText), userInfo: nil, repeats: true)
and implement this method:
func changeText(){
if i>=welcomeContainer.count {
i = 0
}
welcomeLabel.text = welcomeContainer[i]
i += 1
}
when you want to stop it or change the view controller dont forget to call
timer.invalidate()
You can add sleep function
for i in 0..<welcomeContainer.count {
welcomeLabel.text = welcomeContainer[i]
sleep(2) // or sleep(UInt32(0.5)) if you need Double
}
If you want to keep it all inline you can do this:
var loop: ((Int) -> Void)!
loop = { [weak self] count in
guard count > 0 else { return }
//Do your stuff
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
loop(count - 1)
}
}
loop(10) //However many loops you want
With Timer, you should be careful to call invalidate of the Timer in viewDidDisappear or else you may not release the view controller.
Alternatively, you can use a GCD dispatch timer, in which you completely eliminate the strong reference cycle by using [weak self] pattern:
#IBOutlet weak var welcomeLabel: UILabel!
var timer: DispatchSourceTimer!
override func viewDidLoad() {
super.viewDidLoad()
let welcomeStrings = ["Welcome", "Benvenuti", "Bienvenue", "Willkommen", "üdvözlet", "Dobrodošli", "добро пожаловать", "Witajcie", "Bienvenido", "Ласкаво просимо", "Vitajte", "欢迎你来"]
var index = welcomeStrings.startIndex
timer = DispatchSource.makeTimerSource(queue: .main)
timer.scheduleRepeating(deadline: .now(), interval: .seconds(2))
timer.setEventHandler { [weak self] in
self?.welcomeLabel.text = welcomeStrings[index]
index = index.advanced(by: 1)
if index == welcomeStrings.endIndex {
index = welcomeStrings.startIndex // if you really want to stop the timer and not have this repeat, call self?.timer.cancel()
}
}
timer.resume()
}
Marked answer doesn't delay loop iterations and you still get just the last value in the label.text.
You can solve it like this:
func showWelcome(_ iteration: Int = 0) {
let i = iteration>=self.welcomeContainer.count ? 0 : iteration
let message = self.welcomeContainer[i]
self.delay(2){
self.welcomeLabel.text = message
return self.showWelcome(i + 1)
}
}
Usage:
showWelcome()
I created a timer in one class, and tried to do something else in another class while timer works, and do other thing while timer stops. For example, show every second when timer works. I simplified the code as below. How to realize that?
import Foundation
import UIView
class TimerCount {
var timer: NSTimer!
var time: Int!
init(){
time = 5
timer = NSTimer.scheduledTimerWithTimeInterval( 1.0 , target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update(){
if(time > 0) {
time = time - 1
// do something while timer works
}
else{
timer.invalidate()
timer = nil
time = 5
}
}
}
class Main: UIView {
var Clock: TimerCount!
override func viewDidLoad() {
Clock = TimerCount()
//? do something else while clock works
// ? do other things while clock stops
// FOR EXAMPLE: show every second when timer works
if(Clock.time > 0){
println(Clock.time)
}else{
println("clocker stops")
}
}
}
viewDidLoad is most likely only going to be called once. You could simply make the update method be in your Main object and then pass that instance of main and that update method into the scheduledTimerWithTimerInterval call. Otherwise you need a new method in the Main class to call from the timerCount class and pass in the int for the current time.
This is in your Main class:
func updateMethodInMain(timeAsInt: Int){
//do stuff in Main instance based on timeAsInt
}
This is what you have in your timer class:
func update(){
if(time > 0) {
time = time - 1
instanceNameForMain.updateMethodInMain(time)
}
else{
timer.invalidate()
timer = nil
time = 5
}
}
}