Rutger Huijsmans
Rutger Huijsmans

Reputation: 2408

UILabel is not appearing in delayed sequence as expected

I want to make my UILabels appear in a delayed sequence. Thus one after another. The code below works when I make them fade in using the alpha value but it doesn't do what I want it to do when I use the .hidden property of the UILabels.

The code makes my UILabels appear at all the same time instead of sum1TimeLabel first after 5 seconds, sum2TimeLabel second after 30 seconds and finally sum3TimeLabel third after 60 seconds. What am I doing wrong?

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    UIView.animateWithDuration(5.0, animations:  {
        self.sum1TimeLabel!.hidden = false;
    })

    UIView.animateWithDuration(30.0, animations: {
        self.sum2TimeLabel!.hidden = false;
    })

    UIView.animateWithDuration(60.0, animations: {
        self.sum3TimeLabel!.hidden = false;
    })
}

Upvotes: 0

Views: 95

Answers (3)

user3441734
user3441734

Reputation: 17534

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        label1.alpha = 0
        label2.alpha = 0
        label3.alpha = 0
        UIView.animateWithDuration(5.0, animations: { () -> Void in
            print("animation on label1")
            self.label1.alpha = 0.2
            }) { (b) -> Void in
                print("complete on label1")
                self.label1.alpha = 1
                UIView.animateWithDuration(5.0, animations: { () -> Void in
                    print("animation on label2")
                    self.label2.alpha = 0.2
                    }, completion: { (b) -> Void in
                        print("complete on label2")
                        self.label2.alpha = 1
                        UIView.animateWithDuration(5.0, animations: { () -> Void in
                            print("animation on label3")
                            self.label3.alpha = 0.2
                            }, completion: { (b) -> Void in
                                print("complete on label3")
                                self.label3.alpha = 1
                        })
                })
        }
        // 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.
    }

}

You are NOT able to animate hidden property. there is two states only. how to change hidden continuously with time? instead you can animate alpha :-). try change the alpha value inside animation block to 0, or to 1.

Upvotes: 0

kabiroberai
kabiroberai

Reputation: 2913

As explained by SO user @matt in this answer, you can simply create a delay function with Grand Central Dispatch instead of using animateWithDuration, as animateWithDuration is usually meant for animating things over a period of time, and since the value you are animating is a Bool, it can't animate it.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    delay(5.0) {
        self.sum1TimeLabel?.hidden = false
    }

    delay(30.0) {
        self.sum2TimeLabel?.hidden = false
    }

    delay(60.0) {
        self.sum3TimeLabel?.hidden = false
    }
}

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

Upvotes: 5

Usama
Usama

Reputation: 1965

// Try this

override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)
   self.sum1TimeLabel!.hidden = true;
   self.sum2TimeLabel!.hidden = true;
   self.sum3TimeLabel!.hidden = true;

   UIView.animateWithDuration(5.0, animations: {

   }, completion: {
       self.sum1TimeLabel!.hidden = false;
   })

   UIView.animateWithDuration(30.0, animations: {

   }, completion: {
       self.sum2TimeLabel!.hidden = false;
   })

   UIView.animateWithDuration(60.0, animations: {

   }, completion: {
       self.sum3TimeLabel!.hidden = false;
   })
}

Upvotes: 0

Related Questions