导航 push和pop动画

导航 push和pop动画
一、push默认动画效果
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;

[self.contentView.layer addAnimation:transition forKey:nil];
[self.contentView addSubview:self.productDetailController.view];

注:self.contentView是工程中的UIView,self.productDetailController是工程中的controller只需要把两者替换成合适的内容即可用

二、pop默认动画效果
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
transition.delegate = self;

[self.view.superview.layer addAnimation:transition forKey:nil];
[self.view removeFromSuperview];

三、翻书效果的动画
UIViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView * container = [transitionContext containerView];

[container addSubview:toVC.view];
[container bringSubviewToFront:fromVC.view];

// 改变m34
CATransform3D transfrom = CATransform3DIdentity;
transfrom.m34 = -0.002;
container.layer.sublayerTransform = transfrom;

// 设置archPoint和position
CGRect initalFrame = [transitionContext initialFrameForViewController:fromVC];
toVC.view.frame = initalFrame;
fromVC.view.frame = initalFrame;
fromVC.view.layer.anchorPoint = CGPointMake(0, 0.5);
fromVC.view.layer.position = CGPointMake(0, initalFrame.size.height / 2.0);

// 添加阴影效果
CAGradientLayer * shadowLayer = [[CAGradientLayer alloc] init];
shadowLayer.colors =@[
                      [UIColor colorWithWhite:0 alpha:1],
                      [UIColor colorWithWhite:0 alpha:0.5],
                      [UIColor colorWithWhite:1 alpha:0.5]
                      ];
shadowLayer.startPoint = CGPointMake(0, 0.5);
shadowLayer.endPoint = CGPointMake(1, 0.5);
shadowLayer.frame = initalFrame;

UIView * shadow = [[UIView alloc] initWithFrame:initalFrame];
shadow.backgroundColor = [UIColor clearColor];
[shadow.layer addSublayer:shadowLayer];
[fromVC.view addSubview:shadow];
shadow.alpha = 0;

// 动画
[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:2 animations:^{
    fromVC.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
    shadow.alpha = 1.0;
} completion:^(BOOL finished) {
    fromVC.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
    fromVC.view.layer.position = CGPointMake(CGRectGetMidX(initalFrame), CGRectGetMidY(initalFrame));
    fromVC.view.layer.transform = CATransform3DIdentity;
    [shadow removeFromSuperview];
    
    [transitionContext completeTransition:YES];
}];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容