0

我来自这个问题: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都会初始化一个新对象并将其添加到视图层。我不确定这对记忆有什么影响。它安全吗?如果不安全,我应该怎么做才能不损害内存/性能?

4

1 回答 1

1

一个简单的解决方案是添加一个 CATransition ivar(或属性)并每次重用它。也就是说,我怀疑这会显着影响性能,并且在您确定这是一个问题之前担心性能通常没有用。

所以,简而言之,不要担心。如果您担心它,请制作一个用于这些方法的单个 ivar。

于 2013-05-01T15:32:30.773 回答