0
votes

Randomly my app will not animate at all. My app has a sound bar that gets updated every 0.1 seconds with an animation. Usually when I start the app the animations will work all pretty and everything but then at a random point usually after I transition to another view controller (via setViewControllers) it all of a sudden stops animationg. It's not just this animation but every animation in my app excluding setViewControllers.

Here is my code where I call 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];
    }
}

Here is the code I use to animate the sound bar:

[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];

I have checked and the code is running on the main thread. The CPU was on average around 10% and the Memory was 25 MB. So I am really baffled as to why it stops working sometimes. Has anyone else experienced this?

UPDATE: Sorry to say but the answer I posted was incorrect. It still isn't working. I have removed my answer and attached it bellow:

When creating the animation I added:

[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
Are you loading any of your view controllers from the storyboard or a nib?Milo
@Milo No everything is code basedkeji
IF a set frame or other bit of code hits an error, then the animation instantly completes. You will need to log out and make sure all the frame values are what you expect. If you log out finished in the completion I'll bet it is NO. Probably some bad math in the frame code.Alex Reynolds
@AlexReynolds how can this be true though if the frame goes to the correct position. Only one frame is changed so its not like it is stoping before changing one of the other frames. The animation ends in the right place but just doesn't animate. Also as I said it happens in all animations across my app. Some of those use constants so the frame is not the issuekeji
Where in your code is the animation run? As in, how is it triggered?Lyndsey Scott

1 Answers

0
votes

UPDATED: I just undeleted my answer as it seems one of the properties transitionTransform should have been just transform. So it seems the animation was happening instantly because self.transform != CGAffineTransformIdentity. Even though self was the UINavigationController view it was still the superview of the views I was trying to animate.

Turns out the animation either wasn't being removed or was being removed at the wrong time.

When creating the animation I added:

[transition setDelegate:self];

And then in the delegate method:

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

Where removeTransition was a category method on UIView defined as:

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