美文网首页ios开发的那些坑
iOS-利用CATransition实现push和pop效果

iOS-利用CATransition实现push和pop效果

作者: fly大梦想家 | 来源:发表于2018-07-31 13:56 被阅读3次

场景:需要push和pop动画,但是做界面不愿意创建多个viewController
举例子:A和B界面的跳转


一个控制器的push&pop操作.gif

1.初始化的时候创建界面A

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNav];
    [self creatOrderTableView];
}

2.触发button创建界面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"];  //加这句是因为夜间模式下, 动画过程中当前页面透明度改变,会显示window颜色(最初在appdelegate中设置的白色,此时就会有闪白),所以在这改变一下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];
}

如果界面不是白色的,动画出现闪白,参照3中注释

相关文章

网友评论

    本文标题:iOS-利用CATransition实现push和pop效果

    本文链接:https://www.haomeiwen.com/subject/huthvftx.html