如何在iOS中为ECSlidingViewController构建3D变换?

时间:2014-07-07 08:41:53

标签: ios rotation ecslidingviewcontroller ecslidingviewcontroller-2

我想基于https://github.com/ECSlidingViewController/ECSlidingViewController构建一些特殊动画。

缩放动画如下图所示。

enter image description here

我只想通过PI / 4旋转主视图控制器。如下图所示。

enter image description here

我曾尝试像下面的代码一样进行EndState转换,但它不起作用。

- (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);
}

任何帮助,指针或示例代码片段都会非常感激!

2 个答案:

答案 0 :(得分:1)

我设法做到了。从ECSlidingViewController中给出的缩放动画代码中,不要在

中应用缩放因子
- (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;
}

评论MEZoomAnimationScaleFactor

然后在- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext方法的顶部添加

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

最后,进行所有转换的方法必须是:

- (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);
}

欣赏你的动画:)

答案 1 :(得分:0)

除了ryancrunchi的回答之外,您还需要修改topViewStartingState以将x位置重置为0而不是容器帧的中间位置。这将消除动画开始时的抖动偏移:

更改

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

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

动画中还有一个错误,即在开场动画结束时会出现一个生涩的左菜单。从动画的完成部分复制以下行,以便在动画期间运行:

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

动画功能现在看起来像:

...
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];
        }];
    }
...