我來自這個問題:How to change the Push and Pop animations in a navigation based appUINavigationController動畫變化
我想扭轉推動和流行的動畫。所以,而不是默認動畫到右邊,它應該去左邊,反之亦然。
我創建了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
對象並將其添加到視圖的圖層中。我不確定這對記憶有什麼影響。它是否安全?如果不是,我該怎麼做纔不會傷害記憶/表現?
感謝您的提示「在確定問題發生之前,擔心性能通常不是有用的。」我會嘗試你的建議。 – 2013-05-01 15:59:34
這被廣泛接受爲當今世界具有豐富資源的加速設備的標準做法。儘管如此,保存資源同樣容易實現,這絕對不是壞事...... – HackyStack 2013-05-01 20:08:37