2014-09-01 97 views
0

随机我的应用程序根本不会动画。我的应用程序有一个声音栏,每0.1秒更新一次动画。通常,当我启动应用程序时,动画将会非常漂亮地运行,但通常在我转换到另一个视图控制器(通过setViewControllers)后,随机点会突然停止动画。这不只是这个动画,而是我的应用程序中的所有动画,不包括setViewControllers有时UIView animateWithDuration立即发生

这里是我的代码,我叫setViewControllers

- (void)goToController:(GeneralViewController *)vc animate:(BOOL)animated sub:(BOOL)sub{ 
    if(animated){ 
     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     NSMutableArray *viewController = [NSMutableArray arrayWithArray:[delegate.navController viewControllers]]; 
     if(viewController.count > 0){ 
      viewController[0] = vc; 
     }else{ 
      [viewController addObject:vc]; 
     } 
     CATransition* transition = [CATransition animation]; 
     transition.duration = 0.2; 
     transition.type = kCATransitionPush; 
     if(sub) 
      transition.subtype = kCATransitionFromRight; 
     else 
      transition.subtype = kCATransitionFromLeft; 

     [delegate.navController.view.layer addAnimation:transition forKey:kCATransition]; 
     [delegate.navController setViewControllers:viewController animated:FALSE]; 
     self.colorStyle = MusicPlusColorStyleLight; 
    }else{ 
     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     NSMutableArray *viewController = [NSMutableArray arrayWithArray:[delegate.navController viewControllers]]; 
     if(viewController.count > 0){ 
      viewController[0] = vc; 
     }else{ 
      [viewController addObject:vc]; 
     } 
     [delegate.navController.view.layer removeAnimationForKey:kCATransition]; 
     [delegate.navController setViewControllers:viewController animated:FALSE]; 
    } 
} 

下面是我用动画的声音吧的代码:

[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         meterViewColor.frame = CGRectMake(0, (meterView.frameSizeHeight - (level*meterView.frameSizeHeight)), meterView.frameSizeWidth, (level*meterView.frameSizeHeight)); 
        } 
     completion:nil]; 

我已经检查和代码是在主运行线。 CPU平均约为10%,内存为25 MB。所以我对于它为什么有时会停止工作感到非常困惑。有没有其他人经历过这个?

更新:对不起,但我发布的答案是不正确的。它仍然没有工作。我已删除我的答案和波纹管连接它:

当创建我加入了动画:

[transition setDelegate:self]; 

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [delegate.navController.view removeTransition]; 
} 

- (void) removeTransition { 
    if (!CGAffineTransformIsIdentity(self.transform)) { 
     self.transform = CGAffineTransformIdentity; 
    } 
} 
+0

是你从故事板或笔尖加载任何视图控制器? – Milo 2014-09-02 00:33:52

+0

@Milo没有一切都是基于代码的 – kezi 2014-09-02 00:42:45

+0

如果设置的框架或其他代码点击错误,那么动画立即完成。您需要注销并确保所有帧值都符合您的预期。如果您在完成时退出'完成',我会打赌它是'不'。在框架代码中可能有些不好的数学。 – 2014-09-02 01:30:11

回答

0

修订:我只是未删除我的回答,因为它似乎属性transitionTransform之一应该是刚刚transform 。所以看起来动画是即时发生的,因为self.transform != CGAffineTransformIdentity。尽管self是UINavigationController视图,但它仍然是我试图制作动画的视图的超级视图。

发现动画要么没有被删除,要么在错误的时间被删除。

在创建动画我说:

[transition setDelegate:self]; 

然后在委托方法:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [delegate.navController.view removeTransition]; 
} 

removeTransition已于UIView的一类方法定义为:

- (void)removeTransition { 
    if (!CGAffineTransformIsIdentity(self.transform)) { 
     self.transform = CGAffineTransformIdentity; 
    } 
} 
相关问题