iOS-利用CATransition實現(xiàn)push和pop效果

場景:需要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中注釋

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 人生,就是一條路 途中,會歷經(jīng)坎坷 也會面對風(fēng)雨 一路上,會遇見各種各樣的人 有的和你并肩走到底 有的陪你一段就離...
    A素年槿兮閱讀 1,013評論 1 37
  • As well as all these facilities available here on compus,...
    Aiden拾影閱讀 179評論 0 1
  • 文章是一個戰(zhàn)友的思考,我先轉(zhuǎn)載 先碼后看。 最近學(xué)習(xí)了【奧卡姆剃刀理論】,覺得很有趣,可以應(yīng)用的場景也很多,在網(wǎng)上...
    大偉傳說閱讀 726評論 0 0
  • 初八,上班第二天,花開富貴,肉呈吉祥。過個年回來,藍(lán)雪花盛開,多肉顏色艷麗,尤其火棘紅艷艷的,特別美麗,空調(diào)房特別...
    心境如花閱讀 234評論 0 0

友情鏈接更多精彩內(nèi)容