一谢澈、需求:優(yōu)化系統(tǒng)push pop動(dòng)畫(huà)卡頓問(wèn)題煌贴。
二、實(shí)現(xiàn)思路:UINavigationController子類中攔截push pop方法锥忿,屏蔽系統(tǒng)動(dòng)畫(huà)牛郑,用自己的動(dòng)畫(huà)。
代碼如下:
BaseNavigationController.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BaseNavigationController : UINavigationController
@end
NS_ASSUME_NONNULL_END
BaseNavigationController.m文件
#import "BaseNavigationController.h"
@implementation BaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (animated) {
CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.25;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.layer addAnimation:transition forKey:nil];
}
[super pushViewController:viewController animated:NO];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
if (animated) {
[self.view.layer addAnimation:[self popTransition] forKey:nil];
}
return [super popViewControllerAnimated:NO];
}
- (NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (animated) {
[self.view.layer addAnimation:[self popTransition] forKey:nil];
}
return [super popToViewController:viewController animated:NO];
}
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
if (animated) {
[self.view.layer addAnimation:[self popTransition] forKey:nil];
}
return [super popToRootViewControllerAnimated:NO];
}
- (CATransition *)popTransition {
CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.25;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
return transition;
}
@end
知識(shí)點(diǎn):
Core Animation包括屬性動(dòng)畫(huà)(CAPropertyAnimation)敬鬓、動(dòng)畫(huà)組(CAAnimationGroup)和轉(zhuǎn)場(chǎng)動(dòng)畫(huà)(CATransition)淹朋。屬性動(dòng)畫(huà)是作用圖層(Layer)的動(dòng)畫(huà)屬性,動(dòng)畫(huà)組是前者的組合列林。
不能做動(dòng)畫(huà)的屬性或者圖層之間的關(guān)系改動(dòng)就要用到過(guò)度動(dòng)畫(huà)瑞你。
CATransition邊寫代碼邊加注釋作為介紹:
CATransition *transition = [[CATransition alloc] init];
// 動(dòng)畫(huà)執(zhí)行時(shí)間。測(cè)試時(shí)候想看效果可以把時(shí)間調(diào)長(zhǎng)看看希痴,看的很清晰
transition.duration = 0.25;
// 動(dòng)畫(huà)樣式者甲,下面有列出詳情
transition.type = kCATransitionPush;
// 控制動(dòng)畫(huà)的方向
transition.subtype = kCATransitionFromRight;
// CATransition必須添加到navigationController的view.layer上,這個(gè)很重要F龃础B哺住!
[self.view.layer addAnimation:transition forKey:nil];
CAAnimation.h
/* Common transition types. */
CA_EXTERN CATransitionType const kCATransitionFade
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // 淡入淡出
CA_EXTERN CATransitionType const kCATransitionMoveIn
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // 慢慢進(jìn)入并覆蓋效果
CA_EXTERN CATransitionType const kCATransitionPush
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // 推進(jìn)效果
CA_EXTERN CATransitionType const kCATransitionReveal
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // 揭開(kāi)效果
/* Common transition subtypes. */
CA_EXTERN CATransitionSubtype const kCATransitionFromRight
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
CA_EXTERN CATransitionSubtype const kCATransitionFromLeft
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
CA_EXTERN CATransitionSubtype const kCATransitionFromTop
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
CA_EXTERN CATransitionSubtype const kCATransitionFromBottom
API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
結(jié)合以上說(shuō)明自己運(yùn)行起來(lái)就更明朗啦D凼怠9粽蕖!
(寫的不對(duì)和補(bǔ)充的地方望指出糾正甲献,謝謝T诅汀)