【发布时间】:2014-09-01 23:49:38
【问题描述】:
随机我的应用程序根本不会动画。我的应用程序有一个条形音箱,每 0.1 秒更新一次动画。通常,当我启动应用程序时,动画会一切正常,但通常在我转换到另一个视图控制器(通过setViewControllers)之后,它会突然停止动画。不只是这个动画,我的应用程序中的每个动画,除了setViewControllers。
这是我调用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;
}
}
【问题讨论】:
-
您是从情节提要还是从 nib 加载任何视图控制器?
-
@Milo 不是一切都是基于代码的
-
如果设置的帧或其他代码遇到错误,则动画立即完成。您将需要注销并确保所有帧值都是您所期望的。如果您在完成时注销
finished,我敢打赌它是NO。框架代码中可能有一些糟糕的数学运算。 -
@AlexReynolds 如果框架转到正确的位置,这怎么可能是真的。只有一帧被更改,因此它不像在更改其他帧之一之前停止。动画在正确的位置结束,但只是没有动画。正如我所说,它发生在我应用程序的所有动画中。其中一些使用常量,因此框架不是问题
-
你的代码在哪里运行动画?如题,是如何触发的?
标签: ios objective-c cocoa-touch animation uiview