One of my favourite effect is the so-called crossfade effect.
Well, I made a crossfade animation between two NSStrings in a UIView (actually UILabel or UITextField).
/** Crossfade effect for text.
@param view A UILabel or UITextField that contains the old text.
@param text New string to show.
@param duration Animation duration in milliseconds.
@return Returns true on success, false otherwise.
*/
+ (BOOL)crossFadeCurrentTextInView:(UIView *)view withNewText:(NSString *)text duration:(CGFloat)duration {
if (!view) { return NO; }
// Works only for UILabel or UITextField.
if ([view isKindOfClass:[UILabel class]] || [view isKindOfClass:[UITextField class]]) {
CATransition *animation = [CATransition animation];
animation.duration = duration;
animation.type = kCATransitionFade;
animation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:animation forKey:@"changeTextTransition"];
if ([view isKindOfClass:[UILabel class]]) {
((UILabel *)view).text = text;
} else {
((UITextField *)view).text = text;
}
[view.layer performSelector:@selector(removeAnimationForKey:) withObject:@"changeTextTransition" afterDelay:duration]; // Remove animation.
return YES;
}
return NO;
}
Download the ready-for-use source file (.m) of Crossfade NSString text inside a UILabel or UITextField
No comments:
Post a Comment