您的位置:首页 > 移动开发 > IOS开发

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]; }];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息