Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fade in /out a rectangle or text?

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!

like image 784
Tianzhou Avatar asked Dec 21 '09 12:12

Tianzhou


People also ask

How do you add a fade to text?

Select the type tool and input your text anywhere inside the preview panel. If you want to change the length of your text after you've inserted it, look for a pink clip on your timeline. Drag the clip's end. Now add a fade by heading to the effects panel and typing dissolve into the search box under video transitions.


1 Answers

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

like image 110
Brad Larson Avatar answered Oct 06 '22 01:10

Brad Larson