Alexandre
Alexandre

Reputation: 3

FadeOut/FadeIn transition for label text change

I'm trying to change the text of an UILabel with a little transition (fade out, change text, fade in) but I'm facing some problems. Here is my code:

- (void) setTextWithFade:(NSString *)text {
    [self setAlpha:1];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(fadeDidStop:finished:context:)];
    [self setAlpha:0];
    [UIView commitAnimations];
}

- (void)fadeDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.25];
    [self setAlpha:1];
    [UIView commitAnimations];
}

This code "works" (the effect is working well), but what I can't figure out is how to change the label text in the "fadeDidStop" function... How can I "pass" the text variable from my first function to the second?

Thanks in advance

Upvotes: 0

Views: 1500

Answers (3)

lyonanderson
lyonanderson

Reputation: 2035

You pass the text in the context:

[UIView beginAnimations:nil context:text];

Then in your fadeDidStop method:

NSString *text = (NSString*) context;

Be mindful when passing objects in the context, make sure they are retained properly.

Upvotes: 2

wkw
wkw

Reputation: 3863

...   
 [UIView beginAnimations:nil context:[text retain]];
...


- (void)fadeDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    self.text = (NSStrinhg *)context;
    [context release];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.25];
    [self setAlpha:1];
    [UIView commitAnimations];
}

Upvotes: 0

Morion
Morion

Reputation: 10860

maybe the simpliest way is to declare NSString object in your .h file and use it to store text to change.

Upvotes: -1

Related Questions