0

I try To make 3D Animation in

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

i write 3D Animation in UINavigationController "PushViewController" 3D Animation Happen Throw

- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated transitionDirecton:(CubeNavigationAnimationDirection)direction{

    UIViewController *current = self.visibleViewController;

    viewController.view.frame = current.view.frame;

    CGFloat halfWidth = current.view.bounds.size.width / 2.0;
    CGFloat duration = 0.7;
    CGFloat perspective = -1.0/1000.0;

    UIView *superView = current.view.superview;
    CATransformLayer *transformLayer = [[CATransformLayer alloc] init];
    transformLayer.frame = current.view.layer.bounds;

    [current.view removeFromSuperview];
    [transformLayer addSublayer:current.view.layer];
    [transformLayer addSublayer:viewController.view.layer];
    [superView.layer addSublayer:transformLayer];

    // let's be safe about setting stuff on view's we don't control
    UIColor *originalBackgroundColor = superView.backgroundColor;
    superView.backgroundColor = [UIColor blackColor];

    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    CATransform3D transform = CATransform3DIdentity;



    if (animated) {
        if (direction == CubeNavigationAnimationOutside) {
            transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
            transform = CATransform3DRotate(transform,-M_PI_2, 0, 1, 0);
            transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
        }
        else{
            transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
            transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
            transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
        }
    }

    viewController.view.layer.transform = transform;
    [CATransaction commit];

    [CATransaction begin];
    [CATransaction setCompletionBlock:^(void) {
        [viewController.view.layer removeFromSuperlayer];
        viewController.view.layer.transform = CATransform3DIdentity;
        [current.view.layer removeFromSuperlayer];
        superView.backgroundColor = originalBackgroundColor;
        [superView addSubview:current.view];
        [transformLayer removeFromSuperlayer];
        [self pushViewController:viewController animated:NO];
        self.view.userInteractionEnabled = YES;
    }];

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

    transform = CATransform3DIdentity;
    transform.m34 = perspective;
    transformAnimation.fromValue = [NSValue valueWithCATransform3D:transform];

    transform = CATransform3DIdentity;
    transform.m34 = perspective;

    if (animated) {
        if (direction == CubeNavigationAnimationOutside) {
            transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
            transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
            transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
        }
        else{
            transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
            transform = CATransform3DRotate(transform,-M_PI_2, 0, 1, 0);
            transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
        }
    }



    transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];

    transformAnimation.duration = duration;

    [transformLayer addAnimation:transformAnimation forKey:@"rotate"];
    transformLayer.transform = transform;

    [CATransaction commit];

}

When i try to Reuse This Code in transitionFromViewController

[self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:3.0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{
                                toViewController.view.frame = fromViewController.view.frame;

                                CGFloat halfWidth = fromViewController.view.bounds.size.width / 2.0;
                                CGFloat duration = 0.7;
                                CGFloat perspective = -1.0/1000.0;

                                UIView *superView = fromViewController.view.superview;
                                CATransformLayer *transformLayer = [[CATransformLayer alloc] init];
                                transformLayer.frame = fromViewController.view.layer.bounds;

                                [fromViewController.view removeFromSuperview];
                                [transformLayer addSublayer:fromViewController.view.layer];
                                [transformLayer addSublayer:toViewController.view.layer];
                                [superView.layer addSublayer:transformLayer];

                                // let's be safe about setting stuff on view's we don't control
                                UIColor *originalBackgroundColor = superView.backgroundColor;
                                superView.backgroundColor = [UIColor blackColor];

                                [CATransaction begin];
                                [CATransaction setDisableActions:YES];
                                CATransform3D transform = CATransform3DIdentity;




                                    if (direction == CubeTransitionDirectionFromRight) {
                                        transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
                                        transform = CATransform3DRotate(transform,-M_PI_2, 0, 1, 0);
                                        transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
                                    }
                                    else{
                                        transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
                                        transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
                                        transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
                                    }



                                toViewController.view.layer.transform = transform;
                                [CATransaction commit];

                                [CATransaction begin];
                                [CATransaction setCompletionBlock:^(void) {
                                    [toViewController.view.layer removeFromSuperlayer];
                                    toViewController.view.layer.transform = CATransform3DIdentity;
                                    [fromViewController.view.layer removeFromSuperlayer];
                                    superView.backgroundColor = originalBackgroundColor;
                                    [superView addSubview:fromViewController.view];
                                    [transformLayer removeFromSuperlayer];
                                    toViewController.view.userInteractionEnabled = YES;
                                }];

                                CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

                                transform = CATransform3DIdentity;
                                transform.m34 = perspective;
                                transformAnimation.fromValue = [NSValue valueWithCATransform3D:transform];

                                transform = CATransform3DIdentity;
                                transform.m34 = perspective;


                                    if (direction == CubeTransitionDirectionFromRight) {
                                        transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
                                        transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
                                        transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
                                    }
                                    else{
                                        transform = CATransform3DTranslate(transform, 0, 0, halfWidth);
                                        transform = CATransform3DRotate(transform,-M_PI_2, 0, 1, 0);
                                        transform = CATransform3DTranslate(transform, 0, 0, -halfWidth);
                                    }


                                transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];

                                transformAnimation.duration = duration;

                                [transformLayer addAnimation:transformAnimation forKey:@"rotate"];
                                transformLayer.transform = transform;

                                [CATransaction commit];

                            }
                            completion:^(BOOL finished) {
                                [toViewController didMoveToParentViewController:self];
                                [fromViewController removeFromParentViewController];
                            }];

And 3D Animation happen but toViewController Disappear after animation .. Any one can Help ME ??

Tefa
  • 15
  • 5

1 Answers1

0

You are missing 2 lines before the transitionFromViewController: method call:

[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];

For adding view controller, basically you need to balance the calling of didMoveToParentViewController with addChildViewController (it will automatically call willMoveToParentViewController) before the animation.

And for removal of view controller, you need to balance the calling of removeFromParentViewController (this will call didMoveToParentViewController automatically) with willMoveToParentViewController before the animation.

Take a look at the UIViewController class reference: Implementing a Container View Controller.

Valent Richie
  • 5,226
  • 1
  • 20
  • 21