UIView淡入淡出动画
小小原创,转载请注明出处:http://iphone.xiaoxiaostudio.net
如果你觉得为某个UIView 加载一个全新的View在这个UIView上面时,想要隐藏时setHidden显得太突兀了,我们可以给它增加一些动画,iOS上默认提供了一些动画,效果已经非常好了,而且使用超简单的。
我的作法是自定义一个新的UIView继承自UIView,重载它的setHidden方法,在这里面增加淡入淡出的动画,这样你需要时,就可以使用 这个view的setHidden方法来改变了。
//.h文件
#import <UIKit/UIKit.h> @interface MyView : UIView @end.m文件
@implementation MyView /* 重写hidden方法 */ -(void)setHidden:(BOOL)hidden { if (hidden) { //隐藏时 self.alpha= 1.0f; [UIView beginAnimations:@"fadeOut" context:nil]; [UIView setAnimationDuration:0.7]; [UIView setAnimationDelegate:self];//设置委托 [UIView setAnimationDidStopSelector:@selector(animationStop)];//当动画结束时,我们还需要再将其隐藏 self.alpha = 0.0f; [UIView commitAnimations]; } else { self.alpha= 0.0f; [super setHidden:hidden]; [UIView beginAnimations:@"fadeIn" context:nil]; [UIView setAnimationDuration:0.7]; self.alpha= 1.0f; [UIView commitAnimations]; } } -(void)animationStop { [super setHidden:!self.hidden]; } @end是不是很简单呢。