changjiashuai's blog

Runnig...

The future belongs to those who believe in the beauty of their dreams.


iOS UIViewControllerTransitioning 自定义界面跳转动画

实现UIViewControllerTransitioningDelegate协议

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

自定义动画步骤

  • 实现UIViewControllerAnimatedTransitioning协议
  • 实现方法
Performing a Transition

– animateTransition:  required method
– animationEnded:


Reporting Transition Duration

– transitionDuration:  required method
- (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

Accessing Transition Attributes

   completionCurve  property
   duration  property
   percentComplete  property
   completionSpeed  property


Managing a Transition

  – updateInteractiveTransition:
  – cancelInteractiveTransition
  – finishInteractiveTransition

结合手势基本逻辑处理

- (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
  • 实现可选方法:
Getting the Animator Objects

– animationControllerForPresentedController:presentingController:sourceController:
– animationControllerForDismissedController:


Getting the Interactive Transition Object

– interactionControllerForPresentation:
– interactionControllerForDismissal:
  • 调用
[self presentViewController:vc animated:YES completion:nil];
最近的文章

ffmpeg Documentation

摘要ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ... 描述ffmpeg是一个非常快的视频和音频转换器,还可以从实时音频/视频源获取。它还可以在任意采样率之间转换和调整高质量的视频的多相滤波器 从ffmpeg的输入“文件”(可以是普通文件,管道,网络流,抓设备等) ,由-i选项指定任意数量的读取和写入到...…

ffmpeg继续阅读
更早的文章

比较提交

你可以用git diff来比较项目中任意两个版本的差异。$ git diff master..test上面这条命令只显示两个分支间的差异,如果你想找出‘master’,‘test’的共有父分支和’test’分支之间的差异,你用3个.来取代前面的两个.$ git diff master...testgit diff是一个难以置信的有用的工具,可以找出你项目上任意两点间的改动,或是用来查看别人提交进来的新分支。你通常用git diff来找你当前工作目录和上次提交与本地索引间的差异。$ git ...…

继续阅读