場景:需要push和pop動畫,但是做界面不愿意創(chuàng)建多個viewController
舉例子:A和B界面的跳轉(zhuǎn)

一個控制器的push&pop操作.gif
1.初始化的時候創(chuàng)建界面A
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self creatNav];
[self creatOrderTableView];
}
2.觸發(fā)button創(chuàng)建界面B
- (void)editAddress:(UIButton *)button{
[self creatAddress];
}
- (void)creatAddress{
self.testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LAYOUT_SCREENSIZE_P.width, LAYOUT_SCREENSIZE_P.height)];
self.testView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.testView];
UINavigationBar *tempNavigationBar = [[UINavigationBar alloc] init];
tempNavigationBar.translucent = NO;
tempNavigationBar.barStyle = UIBarStyleDefault;
tempNavigationBar.barTintColor = [UIColor orangeColor];//[CDCommon colorForKey:@"whiteBackgroundColor"];
tempNavigationBar.tintColor = [CDCommon colorForKey:@"navigationListViewTintColor"];
[self.testView addSubview:tempNavigationBar];
tempNavigationBar.frame = CGRectMake(0, StatusBarHeight, LAYOUT_SCREENSIZE_P.width, NavigationBarHeight);
UILabel *titleLabel = BUI_LABEL_NORMAL(@"navigationbarTitleColor", FONT_SIZE_NAVIGATIONLABELTITLE, NSTextAlignmentCenter);
titleLabel.text = @"Address";
titleLabel.frame = CGRectMake(LAYOUT_SCREENSIZE_P.width / 2 - 100, StatusBarHeight, 200, NavigationBarHeight);
UINavigationItem *item = [[UINavigationItem alloc] init];
item.titleView = titleLabel;
[tempNavigationBar setItems:@[item]];
// 欄目按鈕
item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[CDCommon loadAppImage:@"backbutton"] style:UIBarButtonItemStylePlain target:self action:@selector(backOrderAction)];
[self addAnimationByType:kCATransitionPush subType:kCATransitionFromRight duration:0.3];
}
3.動畫方法
// 添加動畫方法
- (void)addAnimationByType:(NSString *)type subType:(NSString *)subType duration:(CFTimeInterval)duration {
[UIApplication sharedApplication].keyWindow.backgroundColor = [CDCommon colorForKey:@"whiteBackgroundColor"]; //加這句是因為夜間模式下, 動畫過程中當(dāng)前頁面透明度改變,會顯示window顏色(最初在appdelegate中設(shè)置的白色,此時就會有閃白),所以在這改變一下window的顏色
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:subType];
[animation setDuration:duration];
[[self.view layer] addAnimation:animation forKey:nil];
}
4.B返回A時的POP操作
- (void)backOrderAction{
for (UIView *view in self.view.subviews) {
if (![view isKindOfClass:[UINavigationBar class]]) {
[view removeFromSuperview];
}
}
[self.view addSubview:self.orderTableView];
[self addAnimationByType:kCATransitionFromTop subType:kCATransitionFromLeft duration:0.3];
}
如果界面不是白色的,動畫出現(xiàn)閃白,參照3中注釋