2

I'm trying to animate a label embedded in a UIView.

this is the code :

-(void)displayText:(NSString*)text {


[label setText:text];


[UIView animateWithDuration:5.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     [labelView setAlpha:1.0];
                 }
                 completion:nil
 ];

[UIView animateWithDuration:5.8
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     [labelView setAlpha:0.0];
                 }
                 completion:nil
 ];
}

To verify, the method is called, i set a breakpoint.

The calls return immediatly but only the end of animations is displayed.

I wired the UIView to the controller.

Pls help, I'm stuck.

Thanks in advance ! Patrick

1 Answer 1

5

Correct,

When you animate views like this the animation doesn't actually happen on screen until the next pass of the runloop (i.e. once your method returns).

UIView will coalesce animations that are programmed sequentially.

Use the completion block to fade back out. The code looks a bit odd but it works great!

[UIView animateWithDuration:5.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     [labelView setAlpha:1.0];
                 }
                 completion:^(BOOL completed){
                     [UIView animateWithDuration:5.8
                                           delay:0.0
                                         options:UIViewAnimationOptionCurveEaseIn
                                      animations:^{[labelView setAlpha:0.0];}
                                      completion:nil];
                 }];

In response to your comments:

The animations won't start until the next run of the runloop. They won't start until your app finishes what its doing. If you wait in the loop you will have the same problem and also freeze up your interface. Consider using individual labels for each letter, and add a progressively bigger delay for each animation. All these animation instructions will be queued up at once and then played out over the course of the next however many seconds. Imagine you are like a movie director, you tell each actor what to do in the next scene. Then, once everyone knows what to do you sit back and yell "action" and watch it all play out.

Sign up to request clarification or add additional context in comments.

12 Comments

hey jack ! Thx for your answer .. But didn't work, just tried it out ;( The animateWithDuration.. method returns again immediatly.
yes. They will return immediately. You are basically giving instructions to UIView to do something at a later point. your calls will complete and when your method returns then the animations will get to work. If you wanted something else to happen please update your question.
ok, maybe I'm getting this totally wrong... What I'm doing : In a loop, I'm calling this method for a sequence of characters. I want to fade-out/in each character one after another. Maybe, do i have to wait in the loop, before calling -(void)displayText:(NSString*)text next time ?
Example : I'm calling the method like :
for (int i=0; i < [words length]; i++) { unichar c = [words characterAtIndex:i]; [self displayText:[NSString stringWithCharacters:&c length:1]]; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.