前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS UIViewControllerTransitioning 自定义界面跳转动画

iOS UIViewControllerTransitioning 自定义界面跳转动画

作者头像
用户3004328
发布2018-09-06 16:52:36
2K0
发布2018-09-06 16:52:36
举报
文章被收录于专栏:增长技术增长技术

实现UIViewControllerTransitioningDelegate协议

UIViewControllerTransitioningDelegate可以控制view controller的出现(presenting) ,消失(dismissing),interacting(交互)动画。

自定义动画步骤

  • 实现UIViewControllerAnimatedTransitioning协议
  • 实现方法
代码语言:javascript
复制
Performing a Transition

– animateTransition:  required method
– animationEnded:


Reporting Transition Duration

– transitionDuration:  required method
代码语言:javascript
复制
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    // 1. Get controllers from transition context
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    // 2. Set init frame for toVC
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height);

    // 3. Add toVC's view to containerView
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];

    // 4. Do animate now
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration
                          delay:0.0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         toVC.view.frame = finalFrame;
                     } completion:^(BOOL finished) {
                         // 5. Tell context that we completed.
                         [transitionContext completeTransition:YES];
                     }];
}

交互动画可以继承UIPercentDrivenInteractiveTransition

代码语言:javascript
复制
Accessing Transition Attributes

   completionCurve  property
   duration  property
   percentComplete  property
   completionSpeed  property


Managing a Transition

  – updateInteractiveTransition:
  – cancelInteractiveTransition
  – finishInteractiveTransition

结合手势基本逻辑处理

代码语言:javascript
复制
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            // 1. Mark the interacting flag. Used when supplying it in delegate.
            self.interacting = YES;
            [self.presentingVC dismissViewControllerAnimated:YES completion:nil];
            break;
        case UIGestureRecognizerStateChanged: {
            // 2. Calculate the percentage of guesture
            CGFloat fraction = translation.y / 400.0;
            //Limit it between 0 and 1
            fraction = fminf(fmaxf(fraction, 0.0), 1.0);
            self.shouldComplete = (fraction > 0.5);

            [self updateInteractiveTransition:fraction];
            break;
        }
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled: {
            // 3. Gesture over. Check if the transition should happen or not
            self.interacting = NO;
            if (!self.shouldComplete || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
                [self cancelInteractiveTransition];
            } else {
                [self finishInteractiveTransition];
            }
            break;
        }
        default:
            break;
    }
}

在ViewController中应用步骤

  • 实现UIViewControllerTransitioningDelegate协议
  • vc.transitioningDelegate = self
  • 实现可选方法:
代码语言:javascript
复制
Getting the Animator Objects

– animationControllerForPresentedController:presentingController:sourceController:
– animationControllerForDismissedController:


Getting the Interactive Transition Object

– interactionControllerForPresentation:
– interactionControllerForDismissal:
  • 调用
代码语言:javascript
复制
[self presentViewController:vc animated:YES completion:nil];
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-07-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现UIViewControllerTransitioningDelegate协议
  • 自定义动画步骤
  • 交互动画可以继承UIPercentDrivenInteractiveTransition
  • 结合手势基本逻辑处理
  • 在ViewController中应用步骤
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档