0

我正在尝试以与使用 CATransition 淡入淡出推送视图控制器相同的方法停止 UIActivityIndi​​catorView 上的动画。

无论我设置持续时间多长,UIActivityIndi​​catorView 都不会停止动画,然后淡入下一个视图控制器,而是在整个淡入淡出过程中继续设置动画。

有什么想法可以让旋转停止,然后以相同的方法推动/淡入淡出吗?

- (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

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];
    });
}
4

1 回答 1

0

我以前遇到过类似的事情。您正在运行的称为同步执行。在同步执行中,UI 不会更新,直到编译器终止函数,也就是}. 你如何解决这个问题?简单的。只需在异步调度中运行任何方法来更新 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];在优先级线程上运行,而函数中的其余代码在默认优先级线程中运行,有效地同时运行代码,就像您期望的那样。

干杯!

于 2013-04-25T01:50:24.433 回答