如何淡入/拉出矩形或文本? [英] How do I fade in /out a rectangle or text?

查看:107
本文介绍了如何淡入/拉出矩形或文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现淡入/淡出矩形或文本的效果。我在我的UIVIew的drawRect:方法中调用CGContextFillRect或CGContextShowText。我想知道是否有一个方法来实现动画没有使用UIView的支持(即[UIView beginAnimations ::]。我想实现的期望效果类似于微软的bach serach引擎,像那些小黑色方块淡入/

解决方案

为什么不想使用UIView的动画块?例如,以下代码将在0.5秒的时间内淡出给定的视图:

  [UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5f];

viewToFadeOut.alpha = 0.0f;

[UIView commitAnimations];

要淡入,



您可以使用手动构造的CABasicAnimation来处理UIView的图层:

  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];

如果你想做的就是淡入/淡出一个视图周围的边框,尝试动画borderColor属性的UIView的层(与上述CABasicAnimation相同,只用 opacity 替换 borderColor 和带有CGColor类型的toValue到 id )。


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

这篇关于如何淡入/拉出矩形或文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆