views:

970

answers:

3

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!

+6  A: 

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).

Brad Larson
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.
Tianzhou Chen
Quartz drawing is an expensive operation, particularly when you are drawing text. If you were using Quartz for each of your text elements within a view, then changing one of those text items, you'd still need to redraw the entire view. The best way to determine whether 100 text labels, animated using Core Animation, will truly be a problem is to simply try it out on the device. However, I can guarantee that manual redrawing of the text for each frame of animation will be much slower.
Brad Larson
A: 

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.

Tianzhou Chen
A: 

Any new ideas? Thanks in advance!

Tianzhou Chen
This is not an answer. Please refrain from trying to "bump" your question, as that's not how this site works.
Brad Larson