Hi all, I want to achieve the effect of fade in / fade out the rect or text. I call CGContextFillRect or CGContextShowText in my UIVIew's drawRect: method. I wonder if there is a way to achieve the animation without using UIView' support(i.e. [UIView beginAnimations::]. The desired effect I want to achieve is similar to what in Microsoft's bing serach engine, like those small black squares fade in/ fade out as you move around the web page. Thanks in advance!
Why do you not want to use UIView's animation blocks? Animating a change in opacity of a view (UILabel or otherwise) with that is pretty easy. For example, the following code will fade out a given view over a duration of 0.5 seconds:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
viewToFadeOut.alpha = 0.0f;
[UIView commitAnimations];
To fade in, simply replace the alpha value of 0.0f with 1.0f.
You can do the same using a manually constructed CABasicAnimation, manipulating the UIView's layer:
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOutAnimation.duration = 0.5f;
fadeOutAnimation.removedOnCompletion = NO;
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0f];
[viewToFadeOut.layer addAnimation:fadeOutAnimation forKey:@"animateOpacity"];
If all you want to do is fade in / out a border around a view, try animating the borderColor property of the UIView's layer (same as the above CABasicAnimation, only replacing opacity
with borderColor
and the toValue with a CGColor cast to id
).
Emm.. What I want is to fade in/fade out the text on the view, not the view itself. Yes, I can create UILabel for every text snippet I want to render on the view and use the UIView animation support. But I wonder if this is an easy and less expensive way to do it. Since I just want to render some static text on the view. If every text is represented by a UILabel, what if there are 100+ text on it? So I choose to use CGContextShowText, but haven't figured out how to achieve fade in/fade out. Thanks again.