在实际的应用中,我们常会看到一些应用中当触发某个事件时,会弹出一个提示框,然后自动消失的效果,其实这种效果的实现是比较简单的,下面我介绍两种简单的方法:
1. 使用UIAlertView来实现,思路是给UIAlertView设置一个延迟时间,然后让其消失(相当于点击了“取消”按钮);
1. 使用UIAlertView来实现,思路是给UIAlertView设置一个延迟时间,然后让其消失(相当于点击了“取消”按钮);
2. 自定义一个动画效果,使用一个UILabel ,并对UILabel 设置一个动画效果。下面来看代码
第一种:// 声明一个UIAlertViewUIAlertView * 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];}];