iOS中自动消失提示框的实现
2015-09-10 16:26
363 查看
在实际的应用中,我们常会看到一些应用中当触发某个事件时,会弹出一个提示框,然后自动消失的效果,其实这种效果的实现是比较简单的,下面我介绍两种简单的方法:
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]; }];
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]; }];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 在ASP.NET使用JavaScript显示信息提示窗口实现原理及代码
- jQuery实现信息提示框(带有圆角框与动画)效果
- jquery插件珍藏(图片局部放大/信息提示框)
- javascript实现确定和取消提示框效果
- 关闭浏览器窗口弹出提示框并且可以控制其失效
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 使用css实现全兼容tooltip提示框
- js实现右下角提示框的方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- PHP 实现类似js中alert() 提示框