首先声明:水平有限,代码比较 low,写出来只是为了加强记忆####
正文###
最近项目遇到了个需求,类似于微博刷新后在 navigationbar 下提示最新数据条数,1秒后隐藏掉
![](https://img.haomeiwen.com/i2374981/c662c3ea4c5a70aa.png)
- 我的思路是用个 label 添加到navigationbar后面,内容视图前面.
![](https://img.haomeiwen.com/i2374981/89286cbc9b12e9aa.png)
如图所示:
从上到下依次为内容视图,提示视图, navigation视图
- 刷新的时候修改偏移量展示.
- 展示完毕后再次修改偏移量隐藏并移除控件
代码部分##
- (void)showNewStatusesCount:(NSInteger)count
{
// 1.创建一个UILabel
UILabel *label = [[UILabel alloc] init];
// 2.显示文字
if (count) {
label.text = [NSString stringWithFormat:@"共有%zd条新的消息", count];
} else {
label.text = @"没有最新的消息";
}
// 3.设置背景
label.backgroundColor = [UIColor orangeColor];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
// 4.设置frame
label.jr_width = self.view.jr_width;
label.jr_height = 35;
label.jr_x = 0;
label.jr_y = 64 - label.jr_height;
// 5.添加到导航控制器的view
[self.navigationController.view insertSubview:label belowSubview:self.navigationController.navigationBar];
// 6.动画
CGFloat duration = 0.5;
[UIView animateWithDuration:duration animations:^{
// 往下移动一个label的高度
label.transform = CGAffineTransformMakeTranslation(0, label.jr_height);
} completion:^(BOOL finished) { // 向下移动完毕
// 延迟delay秒后,再执行动画
CGFloat delay = 1.0;
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
// 恢复到原来的位置
label.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
// 删除控件
[label removeFromSuperview];
}];
}];
}
网友评论