Nstimer Countdown Skip Button - ios

I'm using nstimer to run code that changes text every 60 seconds.
I was wondering if anyone knew of a way to skip an iteration of the 60 seconds. Say a user doesn't want to read the text currently on display, at the moment they still have to wait 60 seconds.
Is there a way to immediately move on to the next 60 second loop at a button tap?
This is what I have so far:
var array : String[]()
var x = 0
#IBAction func playBtnPressed(sender: UIButton)
{
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}
func update()
{
if x < array.count {
let item = array[x]
aLbl.text = array.itemTitle
x += 1
}
}
#IBAction func skipBtnPressed(sender: UIButton)
{
}
Thanks in advance for any guidance you can give me! :)

In your skipBtnPressed() you should first invalidate the timer, update data and then set another timer. Like this
#IBAction func skipBtnPressed(sender: UIButton)
{
timer.invalidate()
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}

Related

All digits not shown in timer countdown

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()
}
}

swift3,my timer can't stop. why?

this is i seting a timer function, the code like this:
#IBAction func start(_ sender: UIButton) {
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
}
#objc func action() {
hoursMinutesSeconds()
if stop == true{
start = false
timer.invalidate()
timer.invalidate()
time = 0
}
}
#IBAction func stop(_ sender: UIButton){
start = false
timer.invalidate()
timer.invalidate()
time = 0
}
but, when i click the stop func, this function not work. mean is the timer not stop. timer stil working…… why? thank you for your time!!
I think you didn't set timer value
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
First you have to declare it as fileprivate as below:
fileprivate var timer = Timer()
#IBAction func start(_ sender: UIButton) {
self.timer = Timer.scheduledTimer(timeInterval: 4,
target: self,
selector: #selector(self.updateTimerForLocation),
userInfo: nil,
repeats: true)
}
#IBAction func stop(_ sender: UIButton) {
timer.invalidate()
}
Ensure that you are invalidating correct instance of Timer.
Like in start function, you didn't assign timer instance to any object, but in stop function,you are using timer vaiable to stop that timer.
Which means you try to invalidate a variable, which never instantiated.
Still try below function to stop timer, after assigning timer variable.
#IBAction func stop(_ sender: UIButton) {
start = false
timer.invalidate()
timer = nil
time = 0
}
Hope this work
you are confusing between variable stop and func stop.
also you dont need 2 parameter to manage stop/start status
--
for your question,
you should retain the timer variable to able to use it in other func
declare a global timer
var timer: Timer!
then
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
now you can invalidate it anywhere

My timer stops after one round

I have this code that is suppossed to make my phone vibrate constantly. but it stops after 1 vibration. Is there something wrong with my timer?
var timer: Timer?
#IBAction func button1(_ sender: UIButton) {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true)
}
What did I do wrong? help? I am also using xcode with swift. and have an iphone 7 plus if that matters.
The timer does not stop vibrating after one round. The timer never vibrates.
What the timer does is to call the doaction method repeatedly. But the doaction method does not perform any vibration. Therefore the only vibration is the single one in button1.
The vibration is only happening when you tap the button, you need the vibration to happen in your "doaction" method:
var timer: Timer?
#IBAction func button1(_ sender: UIButton) {
doaction()
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true)
}
func doaction() {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
}

Stop typewriter animation and show full text in swift

I have the following code to animate the typewriter text in UITextView.
The problem is after the animation starts from the beginning character by character, I want it able to show full text after a button is pressed.
What is the best way to show up the full text after animation starts?
var myText = Array("".characters)
var myCounter = 0
var timer:Timer?
var TextNumber = 0
func fireTimer(){
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: true)
}
func typeLetter(){
if myCounter < myText.count {
myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter])
let randomInterval = Double((arc4random_uniform(8)+1))/190
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: randomInterval, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: false)
} else {
timer?.invalidate()
}
myCounter += 1
}
#IBAction func showFullText(){
//What should i put here?
}
You can do something like this, i.e invalidate the time and convert array into string and place this text
#IBAction func showFullText(){
timer?.invalidate()
let strFromArray = myText.joinWithSeparator(" ")
//In Swift 3, joinWithSeparator becomes joined(separator:)
myTypeWriter.text = strFromArray
}

Swift: Performing a segue when a variable reaches a certain value

I'm trying to segue to another view controller once a counter connected to a NSTimer reaches 0.
Heres my timer code:
var counter = 15
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats: true)
func timerAction() {
--counter
timerLabel.text = "\(counter)"
Assuming you've got a segue set up in the storyboard with identifier mySegueID, use this code:
private var timer:NSTimer! //Make sure this is a property of your class
func timerAction() {
--counter
timerLabel.text = "\(counter)"
if (counter == 0) {
timer.invalidate()
timer = nil
performSegueWithIdentifier("mySegueID", sender: nil)
}
}
This will check if the counter is equal to zero. When it is, it'll stop your NSTimer from firing and call the segue that you've set up in your storyboard.
To trigger the segue, just call the appropriate method when the timer executes.
To be more specific, your timer method could be rewritten. It seems you just want to wait for 15 seconds and then fire the segue, but your code does this by decrementing a counter once a second; a better way is to fire the method after the delay; which you can do more elegantly using GCD:
let delay = dispatch_time(DISPATCH_TIME_NOW, 15 * Int64(NSEC_PER_SEC))
dispatch_after(delay, dispatch_get_main_queue()) { () -> Void in
performSegueWithIdentifier("mySegueID", sender: nil)
}

Resources