我有UIView
名为activityView
,其中包含两个子视图activityIndicator
和cancelOperationsButton
。这些视图嵌入在XIB中,并连接到我的视图控制器。
我有两种处理激活(显示)和停用(隐藏)这两个子视图的方法:
- (void) enableActivityIndicator {
[activityIndicator startAnimating];
[cancelOperationsButton setHidden:NO];
}
- (void) disableActivityIndicator {
[activityIndicator stopAnimating];
[cancelOperationsButton setHidden:YES];
}
这两种方法本身就可以正常工作。
为了给这点做一些改进,我想添加一个动画来淡化这些子视图:
- (void) enableActivityIndicator {
[activityIndicator startAnimating];
[cancelOperationsButton setHidden:NO];
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
activityView.alpha = 1.0f;
[UIView commitAnimations];
}
- (void) disableActivityIndicator {
[UIView beginAnimations:@"fadeOut" context:nil];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
activityView.alpha = 0.0f;
[UIView commitAnimations];
[activityIndicator stopAnimating];
[cancelOperationsButton setHidden:YES];
}
但是动画无法正常工作 - 只有父视图的alpha
属性对透明度产生影响时,子视图才会显示或消失。
我应该如何编写这些方法来获得我追求的淡入淡出效果?
修改
这是一种完美运作的安排:
- (void) enableActivityIndicator {
activityView.alpha = 0.0f;
[activityIndicator startAnimating];
[cancelOperationsButton setHidden:NO];
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
activityView.alpha = 1.0f;
[UIView commitAnimations];
}
- (void) disableActivityIndicator {
[UIView beginAnimations:@"fadeOut" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(disableActivityIndicatorComplete)];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
activityView.alpha = 0.0f;
[UIView commitAnimations];
}
- (void) disableActivityIndicatorComplete {
[activityIndicator stopAnimating];
[cancelOperationsButton setHidden:YES];
}
答案 0 :(得分:3)
在启用时,将视图设置为在动画化为不透明之前清除。
- (void) enableActivityIndicator {
...
activityView.alpha = 0.0f;
[cancelOperationsButton setHidden:NO];
...
在禁用时,请勿在动画完成后隐藏视图。
- (void) disableActivityIndicator {
...
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(hideComplete)];
...
- (void) hideComplete {
[activityIndicator stopAnimating];
[cancelOperationsButton setHidden:YES];
}