Have Label Appear with Delay in Swift

Time delay of texts in label in swift 3

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 = ["AHave Label Appear with Delay in Swiftaa", "bbb", "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.
}
}

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.

Swift iOS- How to hide label then make it appear after a certain time period

@IBAction func buttonTapped(_ sender: UIButton){
self.myLabel.isHidden = true
DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
self.myLabel.isHidden = false
}
}

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
}

How do i make the label change text on its own and delay too - swift?

Create a Timer and make the changes you need inside the function that is called by the timer.
var timer:Timer!
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.countDown), userInfo: nil, repeats: true);

Delayed response when changing label text (Swift)

You have to handle the imageFound==false case similar to the true case in terms of dispatch_async:

if !imageFound {
dispatch_async(dispatch_get_main_queue()) {
self.faceImageView.alpha = 0
self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
print("NO RESULTS!!!!!")
self.faceImageView.alpha = 0
}
}

Why does resizing a label require a delay to update as expected when coming from NotificationCenter?

This sounds very much like an Auto Layout issue. When you don't delay, Auto Layout is adjusting the intrinsic size of the label and running after you modify the frame for the label, so your sizeToFit comes too early and uses the previous intrinsic size.

When you delay by 0.1 seconds, Auto Layout runs first and sets the intrinsic size of the label, and then your sizeToFit() call uses that new intrinsic size to set the frame.


Use Auto Layout

Make things easier on yourself by using Auto Layout. Instead of messing with frame sizes, sizeToFit, and notifications, just set constraints for the leading, trailing, and top edges of your label and Auto Layout will automatically resize your label when the font size changes:

label = UILabel()
label.backgroundColor = .red
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.text = "Test title that should resize"
label.adjustsFontForContentSizeCategory = true
label.textAlignment = .center

let userFont = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title1)
let pointSize = userFont.pointSize
let customFont = UIFont(name: "AvenirNext-DemiBold", size: pointSize)
label.font = UIFontMetrics.default.scaledFont(for: customFont!)

view.addSubview(label)

label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

Passing selected cell's label text, return nil when first time (delay in data sent)

In didSelectRowAt method you are calling self.performSegue first and then setting jobDateValueB. Try moving self.performSegue call to end of the function.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = self.tableView.indexPathForSelectedRow;
let currentCell = self.tableView.cellForRow(at: indexPath!) as UITableViewCell!;
jobDateValueB = currentCell?.textLabel!.text!
self.performSegue(withIdentifier: "showEdit", sender: self)
}

This should solve your problem but it's not the recommended way. Instead of assigning the selected text value to a class variable you pass it as sender. like this.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {        
let indexPath = self.tableView.indexPathForSelectedRow;
let currentCell = self.tableView.cellForRow(at: indexPath!) as UITableViewCell!;
if let value = currentCell?.textLabel?.text? {
self.performSegue(withIdentifier: "showEdit", sender: value)
}
}

And in your prepare method you can do.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showEdit"{
let destinationB = segue.destination as? EditTimeTableVC
if let selectedText = sender as? String {
destinationB?.passedDataB = selectedText
}
}
}


Related Topics



Leave a reply



Submit