我正试图在我的UIActivityIndicatorView上停止动画,方法与使用CATransition fade推送视图控制器的方法相同。
无论停止动画,然后淡入下一个视图控制器,无论我设置持续时间多长,UIActivityIndicatorView都会在整个淡入淡出过程中进行动画制作。
我有什么想法可以让旋转停止,然后按同样的方法推送/淡出?
- (void)pushView:(NSNotification *)notification {
[self.activityIndicator stopAnimating];
NextViewController* nextViewController = [[NextViewController alloc] init];
CATransition *transition = [CATransition animation];
transition.duration = 1.5f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:nextViewController animated:NO];
}
修改1
与David的答案一样,如果我使用stopAnimating
并延迟了几分之一秒,我就能成功dispatch_after
。有没有更好的方法来实现这一目标?:
- (void)pushView:(NSNotification *)notification {
[self.activityIndicator stopAnimating];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.0001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
NextViewController* nextViewController = [[NextViewController alloc] init];
CATransition *transition = [CATransition animation];
transition.duration = 1.5f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:nextViewController animated:NO];
});
}
答案 0 :(得分:0)
我之前遇到过类似的事情。您正在运行的内容称为同步执行。在同步执行中,在编译器终止函数a.k.a. }
之前,UI不会更新。你怎么解决这个问题?简单。只需运行任何方法即可在异步调度:
- (void)pushView:(NSNotification *)notification {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.activityIndicator stopAnimating];
});
NextViewController* nextViewController = [[NextViewController alloc] init];
CATransition *transition = [CATransition animation];
transition.duration = 1.5f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:nextViewController animated:NO];
});
}
糟糕!我忘了把其他代码放在一个简单的发送中!尝试编辑的代码,它应该工作!
这样[self.activityIndicator stopAnimating];
在优先级线程上运行,而函数中的其余代码在默认优先级线程中运行,同时有效地运行代码,就像你期望的那样到。
干杯!