话不多说,直接上代码
-(void)showNewStatusesCount:(int)count
{
// 1.创建一个lable
UILabel * lable = [[UILabel alloc]init];
// 2.显示文字
if (count) {
lable.text = [NSString stringWithFormat:@"共有%d条新消息",count];
}else
{
lable.text = @"没有最新的消息";
}
// 3.设置背景
lable.backgroundColor = [UIColor orangeColor];
lable.textAlignment = NSTextAlignmentCenter;
lable.textColor = [UIColor whiteColor];
// 4.设置frame
int w = self.view.bounds.size.width;
int h = 35;
int x = 0;
int y = 64 - h;
lable.frame = CGRectMake(w, h, x, y);
// 5.添加到导航控制器的view
[self.navigationController.view insertSubview:lable belowSubview:self.navigationController.navigationBar];
// 6.动画
CGFloat duration = 0.7;//持续时间
lable.alpha = 0.0; //慢慢显示
[UIView animateWithDuration:duration animations:^{
lable.transform = CGAffineTransformMakeTranslation(0, h);//从navigationBar下面移出
lable.alpha = 1.0;
} completion:^(BOOL finished) {
// 延迟delay秒后再执行动画,移除lable
CGFloat delay = 1.0;
// UIViewAnimationOptionCurveEaseInOut 快进快出
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
// 恢复到原来位置
lable.transform = CGAffineTransformIdentity;
lable.alpha = 0.0;
} completion:^(BOOL finished) {
// 删除控件
[lable removeFromSuperview];
}];
}];
}