Check if timer is finished - swift

I'm sandboxing a little timer app.
I'm using this cocoapod
This is my code so far:
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
}
}
Now I want to check if the timer is finished (can use this cocoapod built in function: countdownLabel.isFinished() ).
But I have no clue WHERE and HOW I can check this, e.g. in viewDidLoad() I can’t check .isFinished()....
As an example: if(countdownLabel.isFinished()){countdownLabel.text = "Finished"}
WHERE to insert this line in the code??
I would be very happy if you can quickly copy and paste the code as an answer - with the mentioned line correctly inserted :)
Thank you!

You can use delegate method countdownFinished() of CountdownLabel to check when counter finished as mentioned in the example of this pod. Your updated code as below
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
countdownLabel.countdownDelegate = self //>>>>>> added this line to your code
}
}
//>>>>>>added this function to your code
extension ViewController: CountdownLabelDelegate {
func countdownFinished() {
debugPrint("countdownFinished at delegate.")
}
}

I think what you are looking for is a 'listener' for when it reaches 0 ("Finished")
From the docs (they only show on various times, though for 0 you should use
countdownLabel.then(0) { [unowned self] in
countdownLabel.text = "Finished"
}

Related

Passing boolean from one View controller to another, both controlled by a Tab Controller

I've been looking at other questions/answers around this and I can't seem to find an answer thats worked for me. I'm making a game where you click a pizza and you generate money. You start off with only being able to generate one dollar through clicking but as you purchase Upgrades this number gains in value. In theory, when you click the level 1 upgrade button it will send a true value to the main game view, making a click earn 5 dollars instead of 1.
import UIKit
class GameViewController: UIViewController {
var wallet: Int = 0
var totalIncome: Int = 0
var level1UpgradeUsed: Bool = false
#IBOutlet weak var pizzaButton: UIButton!
#IBOutlet weak var labelWallet: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print(level1UpgradeUsed)
// Do any additional setup after loading the view.
}
#IBAction func pizzaClick(_ sender: Any) {
let upgrades = UpgradesViewController()
let level1UpgradeUsed = upgrades.level1used
if level1UpgradeUsed == true {
wallet = wallet + 4
}
wallet = wallet + 1
labelWallet.text = ("$" + String(wallet))
}
In the upgrades controller I'm trying to send the true value once the button is clicked
import UIKit
class UpgradesViewController: UIViewController {
public var level1used: Bool = false
public var level2used: Bool = false
public var level3used: Bool = false
public var level4used: Bool = false
var level5used: Bool = false
#IBOutlet weak var buttonLevel1: UIButton!
#IBOutlet weak var buttonLevel2: UIButton!
#IBOutlet weak var buttonLevel3: UIButton!
#IBOutlet weak var buttonLevel4: UIButton!
#IBOutlet weak var buttonLevel5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let gameViewController = segue.destination as? GameViewController {
gameViewController.level1UpgradeUsed = self.level1used
}
}
#IBAction func Level1Click(_ sender: Any) {
if level1used == false{
buttonLevel1.backgroundColor = UIColor.gray
level1used = true
}
else {
}
}
Im geussing I'm not utilizing the TabBarController to pass data and not sure at all how to go about doing that. I'm new to swift and teaching myself everything so I'm sorry if this seems like a redundant question. It feels like it should be a lot simpler then it looks.

Swift: Tracking User's score

I'm trying to create a Quiz app that consists of two buttons, false and true button. My question is, as I answer more questions in the app, I want the textLabel to keep track of my score. And then when I circle back to the first question again, it should reset. This is my code so far. I'm looking forward to your answers.
class ViewController: UIViewController {
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var progressBar: UIProgressView!
#IBOutlet weak var trueButton: UIButton!
#IBOutlet weak var falseButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
var quizBrain = QuizBrain()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
progressBar.progress = 0.0
}
#IBAction func answerButtonPressed(_ sender: UIButton) {
let userAnswer = sender.currentTitle
let userGotItRight = quizBrain.checkAnswer(userAnswer!)
if userGotItRight {
sender.shortChangeTo(.green)
} else {
sender.shortChangeTo(.red)
}
quizBrain.nextQuestion()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.updateUI()
}
}
func updateUI() {
questionLabel.text = quizBrain.getQuestionText()
trueButton.backgroundColor = UIColor.clear
falseButton.backgroundColor = UIColor.clear
progressBar.progress = 33
scoreLabel.text = "Score: \(quizBrain.getScore)"
}
}
You don't have any code for it yet? You can create a struct like user
struct user {
var score: Int = 0
mutating func answerRight (){
self.score =+ 1 }
mutating func reset (){
self.score = 0 }
}
But I see this in your code "Score: (quizBrain.getScore)"
This says that quizBrain should know the score. But you never add a value to it So check your quizBrain and create a function or an object that can track the score and then call it here "if userGotItRight {}"

Troubles with starting value using UISlider

Started working on the application. There was the following problem, in Main.storyboard the current value is set for the slider (for the top one it is 1.5 out of 3, and for the bottom one it is 100 out of 200), therefore, in the screenshot, I should have points on the slider track in the middle. I started googling about it, I can't find anything. Xcode 12 problem maybe? If not, please help me write the correct search query. I will be very grateful.
Sorry for my bad English. :)
Here ViewController:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var heightLabel: UILabel!
#IBOutlet weak var weightLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func heightSliderChanged(_ sender: UISlider) {
print(sender.value)
heightLabel.text = String(format: "%.2f", sender.value) + "m"
}
#IBAction func weightSliderChanged(_ sender: UISlider) {
weightLabel.text = String(format: "%.0f", sender.value) + "Kg"
}
}
Property's for first slider:
Property's for second slider:
Yes, it's Xcode issues. You can find the issues list from this link. (https://fahimfarook.medium.com/xcode-12-and-ios-14-developer-bugs-and-issues-ada35920a104).
Create an outlet of both slider and set value through coding.
#IBOutlet weak var firstSlider: UISlider!
#IBOutlet weak var secondSlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
firstSlider.minimumValue = 0
firstSlider.maximumValue = 3
firstSlider.value = 1.5
secondSlider.minimumValue = 0
secondSlider.maximumValue = 200
secondSlider.value = 100
}

comparing local images swift

"Optional type Bool cannot be used as a boolean; test for !nil instead"
Is the error I'm getting
I'm trying to make a "slot machine" app, very basic,
You press the UIButton, and the three images should all change, randomly, if the 3 matches, print "You won!"
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
#IBOutlet weak var rollBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll = ImgArray().getRandomImage()
img1.image = randomRoll
img2.image = randomRoll
img3.image = randomRoll
if (img1.image! == img2 && img3) {
print("You won!")
}
}
}
If your images are in an array, why not get the indexes of the images and compare that. Or you could convert the images to base64 strings and compare those.
Edit: Not knowing much about your ImgArray class, this may work for you:
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll1 = ImgArray().getRandomImage()
let randomRoll2 = ImgArray().getRandomImage()
let randomRoll3 = ImgArray().getRandomImage()
img1.image = randomRoll1
img2.image = randomRoll2
img3.image = randomRoll3
let imgIndex1 = ImgArray().indexOf(randomRoll1)
let imgIndex2 = ImgArray().indexOf(randomRoll2)
let imgIndex3 = ImgArray().indexOf(randomRoll3)
if (imgIndex1 == imgIndex2 && imgIndex3) {
print("You won!")
}
}

How can I add several UIStepper Values to one Label?

I'm working right now on my first Swift project. I've got 2 stepper and one label - both stepper are sending their values to it. How can I add the value of the second stepper to the label, in which the value of the first stepper is already? Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
}
Thank you!
If you want to combine the two values to ONE String and show this String on your Label, than you have to create a new function that does this for you. I added such a function to your code:`
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
// valueLabel.text = Int(sender.value).description
addValuesToASumAndPutItIntoTheLabel()
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
// valueLabel.text = String(sender.value)
addValuesToASumAndPutItIntoTheLabel()
}
func addValuesToASumAndPutItIntoTheLabel() {
let summe : Int = Int(stepper.value + stepper2.value)
valueLabel.text = summe.description
}
}`