我来自这个问题:How to change the Push and Pop animations in a navigation based app
我想反转 push 和 pop 的动画。因此,默认动画不是向右,而是向左,反之亦然。
我创建了一个子类UINavigationController
并覆盖了 push 和 pop 方法。
这是代码:
- (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;
}
到目前为止,这些都运行良好。
在这些方法中,这条线[self.view.layer addAnimation:transition forKey:nil];
让我感到困惑。我看到每次调用方法时CATransition
都会初始化一个新对象并将其添加到视图层。我不确定这对记忆有什么影响。它安全吗?如果不安全,我应该怎么做才能不损害内存/性能?