有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