0

My objective is to get label0 to dynamically change text as label1 changes transparency. Here is a section of the code i used for label1's animation:

        UIView.animateWithDuration(2.0, animations: { () -> Void in
        self.label1.alpha = 0.0
        //this is what happens after "tIme"
        dispatch_after(tIme1, dispatch_get_main_queue()) {
            UIView.animateWithDuration(2.0, animations: { () -> Void in
                self.label1.alpha = 1.0
            })
            dispatch_after(tIme20, dispatch_get_main_queue()) {
                UIView.animateWithDuration(5.0, animations: { () -> Void in
                    self.label1.alpha = 0.0
                })
            }
        }

    })

It basically it starts from being transparent, after 1s it fades into completely visible, and after 20s it goes back to being transparent. THIS PART OF CODE WORKS ^

The part that doesn't work is the one that is supposed to make label0 change:

        if(label1.alpha == 0.0)    {
        self.label0.text = "transparent"
    }
    else {
        self.label0.text = "visible"
    }

What i'd like this to be doing is pretty self explanatory. If label1 is transparent make label0 say "transparent" or else say "visible". The problem is that it keeps saying "transparent" even when label1 is visible.

How do i fix this?

If you need any clarification, don't hesitate to ask.

EDIT: Its not possible for me to change the animation, because what i need to do is display some text on ViewDidLoad, and other text on ViewDidAppear. I could to that easily, but since its different text, the first time i open the app it would overlap. So what i need to do is wait for the first text to disappear, and then check if its invisible. If it is, on ViewDidAppear, i can display my second text.

ChubbyChocolate
  • 246
  • 2
  • 3
  • 13

1 Answers1

0

the problem is related to these lines

dispatch_after(tIme1, dispatch_get_main_queue()) {

what this line is telling is "dispatch this block to the main thread after tIme1 seconds":

 UIView.animateWithDuration(2.0, animations: { () -> Void in
      self.label1.alpha = 1.0
 })

First problem here is that dispatch_after does not guarantees that the block will run after tIme1. The line guarantees that the block will be dispatched to the main thread in tIme1 seconds. iOS will say when it will run. Then you are running another block that is asynchronous and will run whenever iOS wants it to run.

How can you test if alpha is 0 or 1 with all this uncertainties?

And also, I am pretty sure that the label's text property is not animatable. Text will simply abruptly change from one thing to another.

What you need is to concatenate all animations using this:

UIView.animateWithDuration(2.0, animations: {
  self.label1.alpha = 0.0
  self.label0.text = "transparent"

  }, completion: { // first animation is complete... running this block
    (value: Bool) in

    UIView.animateWithDuration(2.0, animations: {
      self.label1.alpha = 1.0
      self.label0.text = "visible"

      }, completion: { // second animation is complete... running this block
        (value: Bool) in

        UIView.animateWithDuration(20.0, animations: {
          self.label1.alpha = 0.0
          self.label0.text = "transparent"
          })
    })
})
Duck
  • 34,902
  • 47
  • 248
  • 470
  • Its not possible for me to change the animation, because what i need to do is display some text on ViewDidLoad, and other text on ViewDidAppear. I could to that easily, but since its different text, the first time i open the app it would overlap. So what i need to do is wait for the first text to disappear, and then check if its invisible. If it is, on ViewDidAppear, i can display my second text. – ChubbyChocolate Sep 05 '15 at 15:50
  • ViewDidLoad is a phase where nothing is displayed on the screen yet. How you intend to display text at this phase if nothing is visible yet? – Duck Sep 05 '15 at 15:55
  • The text and animation are already on ViewDidLoad and its working – ChubbyChocolate Sep 05 '15 at 16:26