4

当您单击应用程序图标时,iPad iOS 7 App Store 有一个非常酷的动画(当图标较小时,从精选列表中,而不是搜索结果)。这是它的图片:

在此处输入图像描述

基本上,图标会同时翻转和扩大。

在此处输入图像描述

它后面有一个渐变,内容视图更小。

到目前为止,我有一个自定义的 VC 过渡设置,并且我的放大部分工作正常,但我无法翻转。如何模仿 App Store 动画?

这是我到目前为止的代码:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIView *inView = [transitionContext containerView];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *fromView = [fromVC view];
UIView *toView = [toVC view];
toView.frame = [transitionContext finalFrameForViewController:toVC];

// Take a snapshot of the new view being presented
UIGraphicsBeginImageContextWithOptions(toView.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[fromView.layer renderInContext:ctx];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Add the snapshot view and animate its appearance
UIImageView *intermediateView = [[UIImageView alloc] initWithImage:snapshot];
[inView addSubview:intermediateView];
[self calculateSourceRectInView:inView];
intermediateView.frame = self.sourceRect;

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    intermediateView.layer.transform = CATransform3DMakeRotation(-1.0 * -M_PI_2, 0.0, 1.0, 0.0);
    intermediateView.frame = toView.frame;
} completion:^(BOOL finished) {
    [intermediateView removeFromSuperview];

    if ([transitionContext transitionWasCancelled]) {
        [transitionContext completeTransition:NO];
    } else {
        [inView addSubview:toView];
        [fromView removeFromSuperview];
        [transitionContext completeTransition:YES];

        // Now this is a pushed view, we allow interactive
        // transitioning back to the parent view.
        self.interactiveTransition = [EBInteractiveZoomTransition new];
        [self.interactiveTransition wireToViewController:toVC];
    }
}];
}
4

3 回答 3

3

试试这个方法...

//set Intial Frame of view

[UIView transitionWithView: self.view
                  duration: 1.5f
                   options: UIViewAnimationOptionTransitionFlipFromRight
                animations: ^(void)
 {
 }
                completion: ^(BOOL isFinished)
 {
      // set the Final Frame of the View
 }];
于 2013-10-04T04:07:59.143 回答
3

我在 iPad App Store 应用程序中拍摄了动画视频,它看起来不像 UIView 过渡。如果你慢慢回放,看起来两个动画同时发生:1)图标旋转到大约 90 度,缩放,平移 2)细节淡入,旋转一点,缩放,平移到最终目的地。所以细节不会在图标停止的地方继续。

我认为这就是为什么尝试使用视图动画执行此操作时看起来很奇怪的原因。

要实现更连续的视图之间的翻转,请参见下面的代码。它基本上分几个步骤完成:1)将 backView 定位到 frontView 所在的位置 2)动画 frontView 旋转和缩放一半 3)将 backView 变换设置为与 frontView 相同 4)显示 backView 5)动画 backView 旋转和缩放剩下的路

翻转基本上是相反的。效果很好。

// flip and scale frontView to reveal backView to the center of the screen
// uses a containerView to mark the end of the animation
// parameterizing the destination is an exercise for the reader
- (void)flipFromFront:(UIView*)frontView toBack:(UIView*)backView
{
    float duration = 0.5;

    // distance from center of screen from frontView
    float dx = self.view.center.x - frontView.center.x;
    float dy = self.view.center.y - frontView.center.y;

    // this prevents any tearing
    backView.layer.zPosition = 200.0;

    // hide the backView and position where frontView is
    backView.hidden = NO;
    backView.alpha = 0.0;
    backView.frame = frontView.frame;

    // start the animation
    [UIView animateKeyframesWithDuration:duration
                                   delay:0.25
                                 options:UIViewKeyframeAnimationOptionCalculationModeCubic
                              animations:^{
                                  // part 1.  Rotate and scale frontView halfWay.
                                  [UIView addKeyframeWithRelativeStartTime:0.0
                                                          relativeDuration:0.5
                                                                animations:^{
                                                                    // get the transform for the blue layer
                                                                    CATransform3D xform = frontView.layer.transform;
                                                                    // translate half way
                                                                    xform = CATransform3DTranslate(xform, dx/2, dy/2, 0);
                                                                    // rotate half way
                                                                    xform = CATransform3DRotate(xform, M_PI_2, 0, 1, 0);
                                                                    // scale half way
                                                                    xform = CATransform3DScale(xform, 1.5, 1.5, 1);
                                                                    // apply the transform
                                                                    frontView.layer.transform = xform;
                                                                }];

                                  // part 2. set the backView transform to frontView so they are in the same
                                  // position.
                                  [UIView addKeyframeWithRelativeStartTime:0.5
                                                          relativeDuration:0.0
                                                                animations:^{
                                                                    backView.layer.transform = frontView.layer.transform;
                                                                    backView.alpha = 1.0;
                                                                }];

                                  // part 3.  rotate and scale backView into center of container
                                  [UIView addKeyframeWithRelativeStartTime:0.5
                                                          relativeDuration:0.5
                                                                animations:^{
                                                                    // undo previous transforms with animation
                                                                    backView.layer.transform = CATransform3DIdentity;
                                                                    // animate backView into new location
                                                                    backView.frame = self.containerView.frame;
                                                                }];
                              } completion:^(BOOL finished) {
                                  self.displayingFront = !self.displayingFront;
                              }];
}

// flip from back to front
- (void) flipFromBack:(UIView*)backView toFront:(UIView*)frontView
{
    float duration = 0.5;

    // get distance from center of screen to destination
    float dx = self.view.center.x - frontView.center.x;
    float dy = self.view.center.y - frontView.center.y;

    backView.layer.zPosition = 200.0;
    frontView.hidden = YES;

    // this is basically the reverse of the previous animation
    [UIView animateKeyframesWithDuration:duration
                                   delay:0
                                 options:UIViewKeyframeAnimationOptionCalculationModeCubic
                              animations:^{
                                  [UIView addKeyframeWithRelativeStartTime:0.0
                                                          relativeDuration:0.5
                                                                animations:^{
                                                                    CATransform3D xform = backView.layer.transform;
                                                                    xform = CATransform3DTranslate(xform, -dx/2, -dy/2, 0);
                                                                    xform = CATransform3DRotate(xform, M_PI_2, 0, 1, 0);
                                                                    xform = CATransform3DScale(xform, 0.75, 0.75, 1);
                                                                    backView.layer.transform = xform;
                                                                }];

                                  [UIView addKeyframeWithRelativeStartTime:0.5
                                                          relativeDuration:0.0
                                                                animations:^{
                                                                    backView.alpha = 0.0;
                                                                    frontView.hidden = NO;
                                                                }];

                                  [UIView addKeyframeWithRelativeStartTime:0.5
                                                          relativeDuration:0.5
                                                                animations:^{
                                                                    self.hiddenView.alpha = 0.0;
                                                                    frontView.layer.transform = CATransform3DIdentity;
                                                                }];
                              } completion:^(BOOL finished) {
                                  self.displayingFront = !self.displayingFront;
                              }];
}
于 2014-02-26T19:52:03.723 回答
1

试试这个代码

 [UIView transitionFromView:'yourOriginView'
                        toView:'yourDestination'
                      duration:0.65f
                       options:UIViewAnimationOptionTransitionFlipFromLeft/
                    completion:^(BOOL finished){
//    Do your presentation here
                    }];
于 2013-10-04T04:25:27.870 回答