0

I am coming from this question: How to change the Push and Pop animations in a navigation based app

I wanted to reverse the animation for push and pop. So that, instead of the default animating to Right, it should go to the Left and vice versa.

I created a subclass of UINavigationController and override the push and pop methods.

Here is the code:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    CATransition* transition = [CATransition animation];
    transition.duration = 0.4;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;//kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    [self.view.layer addAnimation:transition forKey:nil];

    [super pushViewController:viewController animated:NO];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    CATransition* transition = [CATransition animation];
    transition.duration = 0.4;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;//kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    [self.view.layer addAnimation:transition forKey:nil];

    UIViewController *poppedVC = [super popViewControllerAnimated:NO];

    return poppedVC;
}

So far, these are working perfectly.

In these methods, the line [self.view.layer addAnimation:transition forKey:nil]; is making me confused. I see that on every call to the methods a new CATransition object is initialized and added to the layer of the view. I am not sure whats the effect of this on memory. Is it safe and if not, what should I do so that it won't harm memory/performance?

Community
  • 1
  • 1
Abdullah Umer
  • 3,788
  • 5
  • 31
  • 56

1 Answers1

1

An easy solution would be to add a CATransition ivar (or property) and reuse that every time. That said, I doubt this will affect performance noticeably and it's not usually useful to worry about performance until you've established that it is an issue.

So, in short, don't worry about it. If you ARE worried about it, make a single ivar that is used in each of these methods.

HackyStack
  • 4,499
  • 2
  • 19
  • 28
  • Thanks for the tip "it's not usually useful to worry about performance until you've established that it is an issue.". I will try your suggestion. – Abdullah Umer May 01 '13 at 15:59
  • 1
    This is widely accepted as standard practice in today's world of souped up devices with plentiful resources. It's never bad to conserve resources where it's just as easy to implement though... – HackyStack May 01 '13 at 20:08