UIView模拟导航栏的push和pop动画

有2个view, 其中第二个view在第一个view上, 第二个view的进场动画是以导航栏的push样式, 离场动画是pop方式.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *orangeView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView * red = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 200)];
    red.backgroundColor = [UIColor redColor];
    red.clipsToBounds = YES;
    [self.view addSubview:red];
    self.redView = red;

    UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
    orangeView.backgroundColor = [UIColor orangeColor];
    orangeView.hidden = YES;
    self.orangeView = orangeView;

    [self.redView addSubview:self.orangeView];

}
- (IBAction)push:(id)sender {

    [self pushView:self.orangeView];

}

- (IBAction)pop:(id)sender {

    [self navPopView:self.orangeView];

}



//模拟Push
- (void)pushView:(UIView *)pushView {

    // 方式1,CATransition
//    CATransition *animation = [CATransition animation];
//    animation.duration = 1.2;
//    animation.timingFunction = [CAMediaTimingFunction
//                                functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    animation.fillMode = kCAFillModeBoth;
//    animation.removedOnCompletion = NO;
//
//    animation.type = kCATransitionPush;
//    animation.subtype = kCATransitionFromRight;
//
//    [pushView.layer addAnimation:animation forKey:nil];
//    pushView.hidden = NO;


    // 方式2:
    pushView.frame = CGRectMake(100, 0, 100, 200);
    pushView.hidden = NO;
    [UIView animateWithDuration:1.2 animations:^{
        pushView.frame = CGRectMake(0, 0, 100, 200);
    } completion:^(BOOL finished) {

    }];


}

//模拟pop
- (void)navPopView:(UIView *)popView {
    // 方式1
//    CATransition *animation = [CATransition animation];
//    animation.duration = 1.2;
//    animation.timingFunction = [CAMediaTimingFunction
//                                functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    animation.fillMode = kCAFillModeBoth;
//    animation.removedOnCompletion = NO;
//    animation.type = kCATransitionPush;
//    animation.subtype = kCATransitionFromLeft;
//    [popView.layer addAnimation:animation forKey:nil];
//    popView.hidden = YES;


    // 方式2
    popView.frame = CGRectMake(0, 0, 100, 200);
    [UIView animateWithDuration:1.2 animations:^{
        popView.frame = CGRectMake(100, 0, 100, 200);
    } completion:^(BOOL finished) {
        popView.hidden = YES;
    }];

}


@end

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值