动画后我的CALayer变换保持不变,但视角消失了

时间:2013-04-19 20:59:35

标签: ios xcode calayer catransform3d

我有以下代码在Y轴上将CALayer旋转-45度:

#define D2R(x) (x * (M_PI/180.0))

- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{        
    CATransform3D transform = CATransform3DMakeRotation(D2R(-45), 0, 1.0, 0);
    transform.m34 = -1.0 / 850;

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"transform"];
    transformAnimation.fillMode = kCAFillModeForwards;
    transformAnimation.removedOnCompletion = NO;
    transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
    transformAnimation.duration = 0.5;

    [self.layer addAnimation:transformAnimation forKey:@"transform"];
}

动画有效,除了它没有透视结束 - 如果我正确理解了事情,就会忽略我的m34设置。

中途:

enter image description here

最后:

enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:2)

动画仅影响动画期间视图的外观。动画结束后,它不会应用于视图。你需要自己做。在添加动画后,我正在猜这样的东西:

self.layer.transform = transform;

您可以立即执行此操作,因为动画将隐藏它直到动画完成。

答案 1 :(得分:0)

试试这个:

- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{        

    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -10 / 850.0;
    transform = CATransform3DRotate(transform, D2R(-45), 0, 1.0, 0);

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"transform"];
    transformAnimation.fillMode = kCAFillModeForwards;
    transformAnimation.removedOnCompletion = NO;
    transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
    transformAnimation.duration = 0.5;

    [self.layer addAnimation:transformAnimation forKey:@"transform"];
}

结束效果是这样的:

enter image description here