有时UIView animateWithDuration会立即发生

时间:2014-09-01 23:49:38

标签: ios objective-c cocoa-touch animation uiview

随机我的应用程序根本不会动画。我的应用程序有一个声音条,每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;
    }
}

1 个答案:

答案 0 :(得分: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;
    }
}
相关问题