这里我已经将这个动画封装成一个类方法,其中颜色,字体大小,背景图片之类的皆可自定义。此处的动画是建立在有navigation下方出现,在继而消失在navigation下方,假如想有什么多的功能,比如点击弹出View跳出一个控制器等等,效果图如下所示
效果图
//.h文件
#import <Foundation/Foundation.h>
@interface YLAnimationShow : NSObject
+ (void)showString:(NSString *)string navigationController:(UINavigationController *)navigationController;
@end
//.m文件
#import "YLAnimationShow.h"
@implementation YLAnimationShow
+ (void)showString:(NSString *)string navigationController:(UINavigationController *)navigationController
{
UILabel *countLabel = [[UILabel alloc] init];
countLabel.backgroundColor = [UIColor purpleColor];
countLabel.width = WindowWidth;
countLabel.height = 35;
countLabel.text = string;
countLabel.textAlignment = NSTextAlignmentCenter;
countLabel.textColor = [UIColor whiteColor];
countLabel.font = [UIFont systemFontOfSize:16];
// 3.添加
countLabel.y = 64 - countLabel.height;
// 将label添加到导航控制器的view中,并且是盖在导航栏下边
[navigationController.view insertSubview:countLabel belowSubview:navigationController.navigationBar];
CGFloat duration = 1.0; // 动画的时间
[UIView animateWithDuration:duration animations:^{
countLabel.transform = CGAffineTransformMakeTranslation(0, countLabel.height);
} completion:^(BOOL finished) {
// 延迟1s后,再利用1s的时间,让label往上移动一段距离(回到一开始的状态)
CGFloat delay = 1.0; // 延迟1s
// UIViewAnimationOptionCurveLinear:匀速
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
countLabel.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[countLabel removeFromSuperview];
}];
}];
}
@end
我的主页
沙漠骑士
网友评论