3d翻转 ios_iOS自定义转场详解04——实现3D翻转效果

这是自定义转场系列的第四篇。由于具有一定的连续性,我会忽略一些基础,所以如果你是第一次看这个系列,可以先过目之前的几篇 ——— UIViewControllerTransitioning的用法 、实现Keynote中的神奇移动效果、实现通过圆圈放大缩小的转场动画。

老规矩,先端上GIF。

How to work

首先在StoryBoard上拖两个UIViewController。并且在第一个VC上放一个button,使用Action Segue连接到第二个VC。

然后回到代码界面。和以往一样,我们需要创建两个文件:一个用于从第一个VC过渡到第二个VC的动画(如push),另一个这是第二个过渡到第一个VC的动画(如pop)。这里不得不说iOS7中引入的这种解耦合的方式,它的意义在于无论在哪儿需要用到转场动画的地方,直接把这两个文件扔过去就行了。

我们创建两个文件:KYPushTransition 和 KYPopTransition 。从名字可以看出,后一个是前一个的反转动画。其实,我们完全可以把两个文件写在一起:KYTransition 。因为两个文件的代码结构几乎别无二致,不同的地方也只要用布尔值区分一下就行了。但这里为了让介绍思路清晰,我们把两个动画分开来实现。

首先是KYPushTransition。

先继承 UIViewControllerAnimatedTransitioning 协议。实现下面两个方法:

- (NSTimeInterval)transitionDuration:(id )transitionContext{

//动画的时间

return 0.6f;

}

- (void)animateTransition:(id )transitionContext{

//动画的逻辑

...

}

下面具体介绍动画的逻辑。

- (void)animateTransition:(id )transitionContext{

//1

FirstViewController *fromVC = (FirstViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

SecondViewController *toVC = (SecondViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *fromView = fromVC.view;

UIView *toView = toVC.view;

UIView *containerView = [transitionContext containerView];

[containerView addSubview:toView];

[containerView sendSubviewToBack:toView];

//2

CATransform3D transform = CATransform3DIdentity;

transform.m34 = -0.002;

containerView.layer.sublayerTransform = transform;

//3

CGRect initialFrame = [transitionContext initialFrameForViewController:fromVC];

fromView.frame = initialFrame;

toView.frame = initialFrame;

//4

[self updateAnchorPointAndOffset:CGPointMake(0.0, 0.5) view:fromView];

//5

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.frame = fromView.bounds;

gradient.colors = @[(id)[UIColor colorWithWhite:0.0 alpha:0.5].CGColor,

(id

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值