CALayer在动画时变换不同

时间:2013-09-26 02:48:08

标签: ios objective-c animation calayer uiviewanimation

这让我发疯了。我正在使用s CATransform3D为我的视图的主图层设置动画(以应用透视效果)。我有一个名为ApplyPerspective的函数可以设置它,ClearPerspective会将图层的转换属性重置为CATransform3DIdentity

这是我的代码:

    ApplyPerspective(self.mainContentView);

    [UIView animateWithDuration:self.animationDuration
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         ClearPerspective(self.mainContentView);
                    }
                     completion:nil];

我遇到的问题是ApplyPerspective应用的透视变换不正确。如果我删除了animateWithDuration调用,但保留了ApplyPerspective调用,则会按预期呈现图层。我不确定为什么动画时行为会发生变化。

我尝试更改持续时间,动画曲线,展平函数调用,但我无法追踪此错误的来源。

更新以添加ApplyPerspective()和ClearPerspective()的主体:

void ApplyPerspective(UIView *aView) {

    // Magic numbers derived using debug sliders
    CGFloat zDist, rotRads, xOffset, yOffset, scale, shift;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        zDist   = 172.5378;
        rotRads = 2.6531;
        xOffset = -13.0;
        yOffset = 402;
        shift   = -241;

    }
    else {
        zDist =     513.4995;
        rotRads =   1.0007;
        xOffset = 0.0;
        yOffset =   -100.0;
        scale = 1.0;
        shift = 0.0;
    }

    CATransform3D p = CATransform3DIdentity;

    p = CATransform3DTranslate(p, xOffset, yOffset, 0.0);

    p.m34 = 1.0 / -zDist;
    p = CATransform3DRotate(p, rotRads, 1.0, 0.0, 0);

    p = CATransform3DTranslate(p, 0, shift, 0.0);

    aView.layer.transform = p;
}

void ClearPerspective(UIView *aView, UIView *parentView) {
    aView.layer.transform = CATransform3DIdentity;
}

这是视图层次结构:mainContentView是ViewController根视图的子视图,它使用自动布局。约束是这样的,mainContentView填充根视图。 mainContentView有一个兄弟图像视图,在动画过程中用作背景。

1 个答案:

答案 0 :(得分:2)

我终于解决了!显然,一些变换可能会影响自动布局,并且布局引擎正在修改mainContentView的帧,即使转换被重置为身份转换。

修复是将-layoutIfNeeded发送到父视图(在我的情况下为self.view)以强制布局,并在动画完成后再次发送。我发布这个,因为其他人可能遇到类似的事情。

如果您的转换效果不正确,请发送-layoutIfNeeded

相关问题