开发者

FadeOut/FadeIn transition for label text change

开发者 https://www.devze.com 2022-12-14 07:57 出处:网络
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:

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];
   开发者_Go百科 [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


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.


...   
 [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];
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消