How do i build 3D transform in iOS for ECSlidingViewController?

302 Views Asked by At

I want to build a some special animation based https://github.com/ECSlidingViewController/ECSlidingViewController.

The Zoom animation is like below image.

enter image description here

I just want to rotate the main view controller by PI / 4. Like below image.

enter image description here

I had tried to EndState transform like below code, but it doesn't work.

- (void)topViewAnchorRightEndState:(UIView *)topView anchoredFrame:(CGRect)anchoredFrame {

CATransform3D toViewRotationPerspectiveTrans = CATransform3DIdentity;
toViewRotationPerspectiveTrans.m34 = -0.003;
toViewRotationPerspectiveTrans = CATransform3DRotate(toViewRotationPerspectiveTrans, M_PI_4, 0.0f, -1.0f, 0.0f);

topView.layer.transform = toViewRotationPerspectiveTrans;
topView.layer.position = CGPointMake(anchoredFrame.origin.x + ((topView.layer.bounds.size.width * MEZoomAnimationScaleFactor) / 2), topView.layer.position.y);
}

Any help, pointers or example code snippets would be really appreciated!

2

There are 2 best solutions below

0
On

I managed to do it. From zoom animation code given in ECSlidingViewController, do not apply zoom factor in

- (CGRect)topViewAnchoredRightFrame:(ECSlidingViewController *)slidingViewController{
    CGRect frame = slidingViewController.view.bounds;

    frame.origin.x    = slidingViewController.anchorRightRevealAmount;
    frame.size.width  = frame.size.width/*  * MEZoomAnimationScaleFactor*/;
    frame.size.height = frame.size.height/* * MEZoomAnimationScaleFactor*/;
    frame.origin.y    = (slidingViewController.view.bounds.size.height - frame.size.height) / 2;

    return frame;
}

by commenting MEZoomAnimationScaleFactor.

Then at the top of - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext method add

[topView.layer setAnchorPoint:CGPointMake(0, 0.5)];
[topView.layer setZPosition:100];

Finally the method doing all the transformation must be :

- (void)topViewAnchorRightEndState:(UIView *)topView anchoredFrame:(CGRect)anchoredFrame {
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -0.003;
    transform = CATransform3DScale(transform, ARDXZoomAnimationScaleFactor, ARDXZoomAnimationScaleFactor, 1);
    transform = CATransform3DRotate(transform, -M_PI_4, 0.0, 1.0, 0.0);
    topView.layer.transform = transform;
    topView.frame = anchoredFrame;
    topView.layer.position  = CGPointMake(anchoredFrame.origin.x, anchoredFrame.size.height/2+anchoredFrame.origin.y);
}

Enjoy your animation :)

0
On

On top of ryancrunchi's answer, you also need to modify the topViewStartingState to reset the x position to 0 and not the middle of the container frame. This removes the jerky offset at the start of the animations:

Change

- (void)topViewStartingState:(UIView *)topView containerFrame:(CGRect)containerFrame {
    topView.layer.transform = CATransform3DIdentity;
    topView.layer.position  = CGPointMake(containerFrame.size.width / 2, containerFrame.size.height / 2);
}

to

- (void)topViewStartingState:(UIView *)topView containerFrame:(CGRect)containerFrame {
    topView.layer.transform = CATransform3DIdentity;
    topView.layer.position  = CGPointMake(0, containerFrame.size.height / 2);
}

There is also a mistake in the animation that causes a jerky left menu at the end of the opening animation. Copy the following line from the completion part of the animation so that it runs during the animation:

[self topViewAnchorRightEndState:topView anchoredFrame:[transitionContext finalFrameForViewController:topViewController]];

The animation function will now look like:

...
if (self.operation == ECSlidingViewControllerOperationAnchorRight) {
        [containerView insertSubview:underLeftViewController.view belowSubview:topView];
        [topView.layer setAnchorPoint:CGPointMake(0, 0.5)];

        [topView.layer setZPosition:100];

        [self topViewStartingState:topView containerFrame:containerView.bounds];
        [self underLeftViewStartingState:underLeftViewController.view containerFrame:containerView.bounds];

        NSTimeInterval duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration animations:^{
            [self underLeftViewEndState:underLeftViewController.view];
            underLeftViewController.view.frame = [transitionContext finalFrameForViewController:underLeftViewController];
            [self topViewAnchorRightEndState:topView anchoredFrame:[transitionContext finalFrameForViewController:topViewController]];
        } completion:^(BOOL finished) {
            if ([transitionContext transitionWasCancelled]) {
                underLeftViewController.view.frame = [transitionContext finalFrameForViewController:underLeftViewController];
                underLeftViewController.view.alpha = 1;
                [self topViewStartingState:topView containerFrame:containerView.bounds];
            }

            [transitionContext completeTransition:finished];
        }];
    }
...