有的时候,我们要在屏幕中现有的视图前方显示一些信息,这些信息本身只起提示作用,并没有什么别的用途,可以用UIView动画效果分别令视图显示出来,或者慢慢消失。
主要代码:
- (void) fadeOut
{
_button.enabled = NO;
[UIView animateWithDuration:5.0 animations:^{
self.imageView.alpha = 1;
} completion:^(BOOL finished) {
_button.enabled = YES;
[_button setTitle:@"fade in" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(fadeIn) forControlEvents:UIControlEventTouchUpInside];
}];
}
- (void)fadeIn
{
_button.enabled = NO;
[UIView animateWithDuration:5.0 animations:^{
self.imageView.alpha = 0;
} completion:^(BOOL finished) {
_button.enabled = YES;
[_button setTitle:@"fade out" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(fadeOut) forControlEvents:UIControlEventTouchUpInside];
}];
}
当用户点击按钮时,迅速将这个按钮禁用,直到动画结束之后,再重新启用它,这样的话就不会在淡入淡出的过程中有点击事件的干扰了。