iOS中自动消失提示框的实现

在实际的应用中,我们常会看到一些应用中当触发某个事件时,会弹出一个提示框,然后自动消失的效果,其实这种效果的实现是比较简单的,下面我介绍两种简单的方法:
1. 使用UIAlertView来实现,思路是给UIAlertView设置一个延迟时间,然后让其消失(相当于点击了“取消”按钮);

2. 自定义一个动画效果,使用一个UILabel ,并对UILabel 设置一个动画效果。下面来看代码

 
 
第一种:
// 声明一个UIAlertView
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名已存在" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
// 显示 UIAlertView
[alert show];
// 添加延迟时间为 1.0 秒 然后执行 dismiss: 方法
[self performSelector:@selector( dismiss:) withObject:alert afterDelay:1.0];
实现 dismiss:方法:
- (void) dismiss:(UIAlertView *)alert{
// 此处即相当于点击了 cancel 按钮
[alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
}
第二种:
// 声明一个 UILabel 对象
UILabel * tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 225, 120, 30)];
// 设置提示内容
[tipLabel setText:@"选择什么呢!"];
tipLabel.backgroundColor = [UIColor blackColor];
tipLabel.layer.cornerRadius = 5;
tipLabel.layer.masksToBounds = YES;
tipLabel.textAlignment = NSTextAlignmentCenter;
tipLabel.textColor = [UIColor whiteColor];
[self.view addSubview:tipLabel];
// 设置时间和动画效果
[UIView animateWithDuration:2.0 animations:^{
tipLabel.alpha = 0.0;
} completion:^(BOOL finished) {
// 动画完毕从父视图移除
[tipLabel removeFromSuperview];
}];

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值