Swift: Incrementing Label with Delay

Swift: Incrementing label with delay

Here is an alternative solution that you could use instead of DispatchQueue:

var percentage = 0
var counter = 0
var timer: Timer?

func incrementLabel(amount: Int) {
counter = amount
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.updateDelay), userInfo: nil, repeats: true)
}

@objc func updateDelay() {
if (counter > 0) {
counter -= 1

percentage += 1
} else {
timer.invalidate()
timer = nil
}
}

incrementLabel(amount: 10)
print(percentage)

This uses Timer in Swift.

How to animate incrementing number in UILabel

You could use a flag to see if it has to go up or down.
Instead of a for loop, use a while loop.
In this way, you are creating a loop that keeps going, so you have to find a way to stop it also, f.e. by a button press.

Delay a certain function without stopping entire program // Swift 4

I'd suggest a little care be exercised to avoid strong references to the view controller.

class ViewController: UIViewController {

var tapCount = 0

// I'd suggest weak reference to timer so that when it's invalidated, this is automatically set to `nil`

weak var timer: Timer?

// if view controller is dismissed, stop the timer (if it hasn't already been stopped)

deinit {
timer?.invalidate()
}

// switch to turn timer on or off has changed

@IBAction func toggleTimer(_ sender: UISwitch) {
timer?.invalidate()

if sender.isOn {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
self?.tapCount += 1
}
}
}
}

But the key points are:

  1. The block-based rendition of timers get you out of the confusing target/selector syntax.

  2. When you use target/selector approach, a running timer keeps a strong reference to the target which means if you dismiss the view controller, the timer will keep going and the view controller won't be released. Using the closure-based rendition of the timer in conjunction with [weak self] pattern ensures that the timer won't keep a strong reference to the view controller.

  3. Because the timer no longer keeps a strong reference to the view controller, we can now use deinit to make sure that if the view controller is dismissed, that the timer will be stopped, too.

Trigger fading label delay from view did load instead of at the end of previous fade

One possible solution that I have used before is to use a loop with an incrementing delay variable feeding into the standard UIView.animate() method. This method does not use completion handlers.

Make an array of the labels:

var labels = [titleOutlet, line1Outlet] // etc...

Use this extension:

extension UIView {
func fadeIn(duration: TimeInterval = 1, delay: TimeInterval = 0) {
self.alpha = 0
UIView.animate(withDuration: duration, delay: delay, options: [], animations: { self.alpha = 1 }, completion: nil)
}
}

Then use it like this:

var delay = 0.2
var duration = 0.5

for label in labels {
label.fadeIn(duration: duration, delay: delay)
delay += 0.2 // Increment delay
}

Incremental number animation

yes this is possible.

add a variable with the number to increment

@property (nonatomic) double currentValue;
@property (nonatomic, retain) NSTimer *timer;

initialize it in viewDidLoad

_currentValue = 0.00;

write a function updateNumber

-(void)updateNumber {
_currentValue = _currentValue + 0.01;
[_label setText:[NSString stringWithFormat:@"%.2f", _currentValue]];

if(_currentValue >= 100.0) { //example to stop incrementation on 100
_timer = nil; // stop the timer
// do your animation
}

}

in your viewDidAppear() or whenever you want to start the counting like this

if(_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 //seconds
target:self
selector:@selector(updateNumber)
userInfo:nil
repeats:true];
}

IOS UILabel doesn't update until i reload the app or clicking elsewhere

after a long search i found out that it's because of a bug in macs with intel HD graphic 3000 so it's a driver bug and the workaround for that is to execute this command which did work for me (thankfully)

defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferEmulationHint 2

or use 1 depending on your hardware

thank you guy for your help :)



Related Topics



Leave a reply



Submit