自定义转场动画

动画效果

不知道哪里不对,做的gif好像比较大,链接github图片链接和直接在简书上传都显示不出来,好像.实在看不到的可以去最底下去我的guthub看gif.

gif01

gif02

gif01
gif01

gif02
gif02

自定义转场动画

  • iOS7 开始,苹果推出了自定义转场的API.用于两个viewController切换之间自定义动画,使我们的切换的效果不单单局限于系统自定义动画.

  • 另外,随着大屏幕的普及,如今的app普遍支持手势滑动返回(一般是左滑),自定义转场动画也支持手势滑动返回.

  • 苹果在 UINavigationControllerDelegate 和UIViewControllerTransitioningDelegate 中给出了几个协议方法,通过返回类型就可以很清楚地知道各自的具体作用。我们自定义的转场方法只需要重载以下的方法就行了.使用准则就是:UINavigationController pushViewController 时重载 UINavigationControllerDelegate 的方法;UIViewController presentViewController 时重载 UIViewControllerTransitioningDelegate 的方法。
    <pre>
    @protocol UIViewControllerAnimatedTransitioning <NSObject>
    // This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
    // synchronize with the main animation.
    -(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
    // This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
    -(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
    @protocol UIViewControllerTransitioningDelegate <NSObject>
    @optional
    -(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
    -(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
    -(nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
    -(nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;
    -(nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0);
    </pre>

  • 具体步骤
    -1、创建继承自 NSObject 并且声明 UIViewControllerAnimatedTransitioning 的的动画类。
    -2、重载 UIViewControllerAnimatedTransitioning 中的协议方法。
    <pre>
    //动画时间
    -(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
    {
    return 0.7f;
    }
    </pre>
    <pre>
    // This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
    -(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
    {
    self.transitionContext = transitionContext;//动画上下文
    FirstViewController *fromVC = (FirstViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    SecondViewController *toVC = (SecondViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *contain = transitionContext.containerView;
    [contain addSubview:fromVC.view];
    [contain addSubview:toVC.view]; //这里必须是添加动画的图层在上面,之前在给pop做动画的时候黏贴,找了好久才发现没动画的原因在这里顺序搞反了,囧

    UIButton *pushButton = fromVC.button;
    UIBezierPath *startpath = [UIBezierPath bezierPathWithOvalInRect:pushButton.frame];

    CGPoint finalPoint;
    //根据终点位置所在象限的不同,计算覆盖的最大半径
    if (pushButton.frame.origin.x > toVC.view.bounds.size.width * 0.5 ) {
    if (pushButton.frame.origin.y < toVC.view.bounds.size.height * 0.5) { //第一象限
    finalPoint = CGPointMake(pushButton.center.x , pushButton.center.y - toVC.view.bounds.size.height);
    } else { //第四现象
    finalPoint = CGPointMake(pushButton.center.x , pushButton.center.y);
    }
    } else {
    if (pushButton.frame.origin.y < toVC.view.bounds.size.height * 0.5) { //第二象限
    finalPoint = CGPointMake(pushButton.center.x - toVC.view.bounds.size.width , pushButton.center.y - toVC.view.bounds.size.height);
    } else { //第三现象
    finalPoint = CGPointMake(pushButton.center.x - toVC.view.bounds.size.width, pushButton.center.y);
    }
    }

    CGFloat radius = sqrt(finalPoint.x * finalPoint.x + finalPoint.y * finalPoint.y); //遮罩的最大半径

    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(pushButton.frame, -radius, -radius)]; //-radius表示增大,+radius表示缩小

    //创建一个 CAShapeLayer 作为 toView 的遮罩。并让遮罩发生 path 属性的动画
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = endPath.CGPath; //将它的 path 指定为最终的 path 来避免在动画完成后会回弹
    toVC.view.layer.mask = maskLayer;

    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.fromValue = (__bridge id _Nullable)(startpath.CGPath);
    maskLayerAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    maskLayerAnimation.duration = [self transitionDuration:self.transitionContext];
    maskLayerAnimation.delegate = self;
    maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [maskLayer addAnimation:maskLayerAnimation forKey:@"push"];
    }
    </pre>
    <pre>
    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //告诉 iOS 这个 transition 完成,清除 fromVC和toVC 的 mask
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
    }
    </pre>

-3、fromVC的控制器的使用
控制器实现UINavigationControllerDelegate的协议就行
<pre>
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//这里有个坑,设置navigationController的代理一定要放在viewWillAppear,而不是viewDidLoad里面.否则,push出去,pop回来,再push,就使用回系统默认的push动画了!!!
self.navigationController.delegate = self;
}
-(nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPush) {
PushAnimation *push = [PushAnimation new];
return push;
}else {
return nil;
}
}
</pre>

  • 自定义转场动画就是以上的步骤,pop也是一样.

UIPercentDrivenInteractiveTransition 配合UIScreenEdgePanGestureRecognizer实现用返回滑动手势控制一个百分比交互式切换的过程动画

-1、创建滑动返回手势
<pre>

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan:)];
    edgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePan];
    }

  • (void)edgePan:(UIGestureRecognizer *)gestureRecognizer
    {
    CGFloat per = [gestureRecognizer locationInView:self.view].x / self.view.bounds.size.width;
    per = MIN(1, MAX(0, per));

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
    _percentInteractiveTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
    [self.navigationController popViewControllerAnimated:YES];
    } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged){
    [_percentInteractiveTransition updateInteractiveTransition:per];
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateCancelled){
    if (per > 0.3) {
    [_percentInteractiveTransition finishInteractiveTransition];
    } else {
    [_percentInteractiveTransition cancelInteractiveTransition];
    }
    _percentInteractiveTransition = nil;
    }
    }
    </pre>

-2、实现UINavigationControllerDelegate中的协议
<pre>

  • (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
    interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController
    {
    return _percentInteractiveTransition;
    }

  • (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
    animationControllerForOperation:(UINavigationControllerOperation)operation
    fromViewController:(UIViewController *)fromVC
    toViewController:(UIViewController *)toVC
    {
    if (operation == UINavigationControllerOperationPop) {
    PopAnimation *pop = [PopAnimation new];
    return pop;
    }else {
    return nil;
    }
    }
    </pre>

代码传送门

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容