Time delay of texts in label in swift 3 - ios

I have a label. And I have 3 strings. I need to display text of 3 strings in the same label with delay of 10 seconds over a infinite loop. How can i solve this with simple animations in swift 3?

That's my solution. Just connect a UILabel to the IBOutlet
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"]
let delayTime = 10.0
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true)
timer.fire()
}
func changeDisplayedText() {
textLabel.text = messages[counter % messages.count]
counter += 1
}
}

This will work for your, connect your outlet properly and declare those string in an array and load it with timer change.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"]
var scrollIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true)
timer.fire()
}
func myDelayedFunction()-> Void {
let count = self.array.count
if scrollIndex == count {
scrollIndex = 0
}
if scrollIndex < count {
if count > 1{
self.label.text = array[scrollIndex]
self.scrollIndex += 1
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Swift Run An NSTimer Automatically

I'm making an app and using NSTimer to make the timer in my app.But I need the NSTimer to run when the scene start.I have two scene in my app a homescreen and the app it's self here is my code viewcontroller.swift (The second scene the first scene in empty) And btw it's my 3 day using swift and i'm in middle school ;).
import UIKit
var Number = 2
var Answer = Number * 2
var score = 0
var scorelabel = "Score: "
var Timer = NSTimer()
var Counter = 10
class SecondViewController: UIViewController {
#IBOutlet weak var RightAndWrongLabel: UILabel!
#IBOutlet weak var TimerLabel: UILabel!
#IBOutlet weak var RightAndWrong: UIImageView!
#IBOutlet weak var ScoreIabel: UILabel!
#IBOutlet weak var UserInputAnswer: UITextField!
#IBOutlet weak var Question: UILabel!
#IBOutlet weak var TimerOut: UIImageView!
var seconds = 0
var timeison = true
#IBAction func ConfirmAnswer(sender: AnyObject) {
let UserAnswer = Int(UserInputAnswer.text!)
if UserAnswer == Answer {
print("Your right")
Number += 2
score += 1
ScoreIabel.text = "Score: \(score)"
Question.text = "\(Number) x 2"
UserInputAnswer.text = ""
Answer = Number * 2
RightAndWrong.image = UIImage(named: "Label")
RightAndWrongLabel.hidden = false
RightAndWrongLabel.text = "Right!"
RightAndWrongLabel.textColor = UIColor(red: 0, green: 225, blue: 0, alpha: 1)
} else {
UserInputAnswer.text = ""
RightAndWrong.image = UIImage(named: "Label")
RightAndWrongLabel.hidden = false
RightAndWrongLabel.text = "Wrong!"
RightAndWrongLabel.textColor = UIColor(red: 225, green: 0, blue: 0, alpha: 1)
}
}
func DisplayTimer() {
Timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateTimer(){
TimerLabel.text = String(Counter--)
}
override func viewDidLoad() {
super.viewDidLoad()
UserInputAnswer.keyboardType = UIKeyboardType.NumberPad
RightAndWrongLabel.hidden = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
3
// 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.
}
*/
}
Swift 2.x :
func DisplayTimer() {
Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer() {
if Counter != 0 { TimerLabel.text = "\(Counter -= 1)"
} else {
Timer.invalidate()
// call a game over method here...
}
}
override func viewDidLoad() {
super.viewDidLoad()
...
// start the timer when this controller shows up
DisplayTimer()
TimerLabel.text = "\(Counter)"
...
}
If you want to start your timer when the scene loads call DisplayTimer() under
override func viewDidLoad() when the view is loaded
Also if you want to update your label every second, the selector in your NSTimer is calling updateCounter not updateTimer

swift - trying to update UILabel with delegate

I´m trying to update my #IBOutlet weak var gameclockLabel: UILabel! from my class Gameclock with delegate.
I have read and tested about a million different ways but can't make it work. I think that the more I read about it the more confused I get.
You can read more about what I'm trying to do here:
swift invalidate timer in function
From the answers in that question I added this: var gameClock = Gameclock() so I was able to start a function in class Gameclock and first I tried to do the same with my class ViewController: UIViewController but that didn't work so that's why I decided to try with delegate instead. Do you think delegate is the right way to go with this?
I'm going to add several timers in separate classes to this later on so perhaps there's a better way.
Would be nice if someone could point me in the right direction. At first I thought this would be not to complicated but seems I was mistaking :)
The complete code is as follows:
import UIKit
protocol test1: class {
func updateLabel()
}
class ViewController: UIViewController, test1 {
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLoad() {
super.viewDidLoad()
gameclockLabel.text = "00:00"
}
var gameClock = Gameclock()
var startstopPushed: Bool = false
#IBOutlet weak var gameclockLabel: UILabel!
#IBOutlet weak var startstop: UIButton!
#IBAction func startStopbutton(sender: AnyObject) {
if startstopPushed == false {
gameClock.startGameclock()
startstop.setImage(UIImage(named: "stop.png"), forState: UIControlState.Normal)
startstopPushed = true
}
else
{
gameClock.stopGameclock()
startstop.setImage(UIImage(named: "start.png"), forState: UIControlState.Normal)
startstopPushed = false
}
}
func updateLabel() {
print("updated")
gameclockLabel.text = gameClock.timeString
}
}
class Gameclock : NSObject {
var gameclockTimer = NSTimer()
var timeString: String = ""
var seconds = 0
var minutes = 0
weak var delegate: test1?
func startGameclock() {
print("start")
gameclockTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateGameclock"), userInfo: nil, repeats: true)
}
func stopGameclock() {
self.gameclockTimer.invalidate()
print("stopp")
}
func updateGameclock() {
seconds += 1
if seconds == 60 {
minutes += 1
seconds = 0
}
let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
timeString = "\(minutesString):\(secondsString)"
print(timeString)
delegate?.updateLabel()
}
}
You haven't actually set your ViewController instance as your GameClocks delegate, so your updateLabel method won't be called;
override func viewDidLoad() {
super.viewDidLoad()
gameclockLabel.text = "00:00"
self.gameClock.delegate=self
}

Swift Version 2 Running Wolf App Issues [duplicate]

This question already has an answer here:
Swift Version 2 Bool [closed]
(1 answer)
Closed 7 years ago.
I'm having some issues running an App using Swift v2. Stopping the tmrRun is one issue. I know how to [tmrRun invalidate] in Objective C but not in Swift v2. Any help would be appreciated.
class ViewController: UIViewController {
#IBOutlet var imvWolf: UIImageView!
#IBOutlet var btnGo: UIButton!
#IBOutlet var btnStop: UIButton!
#IBOutlet var sliSpeed: UISlider!
var pic = 0
var tmrRun: NSTimer?
#IBAction func startRunnng(sender: UIButton)
{
tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
btnGo.userInteractionEnabled = false
btnStop.userInteractionEnabled = true
sliSpeed.userInteractionEnabled = false
}
#IBAction func stopRunnng(sender: UIButton)
{
[tmrRun invalidate]
btnGo.userInteractionEnabled = true
btnStop.userInteractionEnabled = false
sliSpeed.userInteractionEnabled = true
}
func takeABound() -> ()
{
pic += 1;
if (pic == 8){
pic = 0;
}
}
override func viewDidLoad() {
super.viewDidLoad()
pic = 0;
// 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.
}
}
To stop the timer is definitely timername.invalidate(), but remember you have to also stop the animation:
timername.invalidate()
isAnimating = false
If doing this you still have issues I think the problem it's where you're typing it.
Try it using this code:
#IBAction func playButton(sender: AnyObject) {
moveWolf()
}
#IBAction func pauseButton(sender: AnyObject) {
timername.invalidate()
isAnimating = false
}
func moveWolf(){
//Here instead of 0.1 you set the slider value
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
isAnimating = true
}
func doAnimation(){
if counter == 5 {
counter = 1
}
else{
counter++
}
imatge.image = UIImage(named: "picture\(counter).png")
}
Hope that helps you! Good luck!

Show array elements in textView (Swift)

How can I add all the elements from an array to a textView with a 1 second delay?
Language- swift
(Still relatively new to programming)
I think it is you need
class ViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
var array = ["string1", "string2", "string3"]
var i = 0
var str: String = ""
var timer = NSTimer()
func testFunc() {
str += "\(array[i])\n"
textView.text = str
if i == count(array) - 1 {
timer.invalidate()
}
i += 1
}
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("testFunc"), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is Code May It's Help Its Delay 1 Second with DispatchQueue Swift 5
var new : String = ""
for a in self.arrayofData{
new = new + a + " "
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.mytextView.text = self.new
}

access dictionary for use within a countdown in SWIFT

I have a working countdown which you can increase by pushing the add button and thats what the time counts down from (so basically a user can set the countdown)
I would like to display the starting time as 00:00 as it does in my label.
When I click button to increase the countdown it just begins at 1 obviously because at the moment its just an Int
So i had a thought of create a dictionary something like this
var timeDictionary : [Double : Double] = [00 : 00]
I just want to increase to 00:01, 2, 3 when the + button is pressed and start from 00:00. Can anyone help with this if possible please?
this is my full code for the countdown
var timeDictionary : [Double : Double] = [00 : 00]
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self,
selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if countdown > 0 {
countdown--
TimerLabel.text = String(countdown)
} else {
countdown = 0
TimerLabel.text = String(countdown)
}
}
#IBOutlet weak var TimerLabel: UILabel!
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
TimerLabel.text = String(countdown)
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
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.
}
}
The simplest approach is just to format the counter as you display it -
#IBOutlet weak var timerLabel: UILabel!
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if --countdown < 1 {
timer.invalidate()
countdown=0;
}
self.updateTimerLabel();
}
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
self.updateTimerLabel()
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
func updateTimerLabel() {
TimerLabel.text =NSString(format:"%02d:%02d",seconds/60,seconds%60)
}
}
Note that I also changed TimerLabel to timerLabel as the convention is that variables should start with a lowercase letter

Resources