2012-02-18 48 views
4

我试图通过使其从屏幕中心出现,同时将其增大到其全部大小,同时还在3D中围绕x轴旋转来呈现视图方式。当我创建视图时,我会对其应用一个转换以确保它缩小并旋转以启动(它实在太小而实际上不可见),然后我尝试使用如下的CATransform3D:如何在单个动画中缩放和旋转视图

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
CATransform3D transform = CATransform3DRotate(view.layer.transform, M_PI, 1.0, 0, 0); 
transform = CATransform3DScale(transform, 1000, 1000, 1000); 
transform.m34 = 1.0/10000; 
[anim setToValue:[NSValue valueWithCATransform3D:transform]]; 
[anim setDuration:0.75f]; 
anim.removedOnCompletion = NO; 
anim.delegate = self; 
[view.layer addAnimation:anim forKey:@"grow"]; 

然而,该动画不会改变实际转换层的,所以我也这样做:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { 
    view.layer.transform = CATransform3DIdentity; 
    [view.layer removeAllAnimations]; 
} 

设置一旦动画停止变换。然而,这有时会导致动画结束时出现明显的闪烁。我相信这是因为动画将原始变换放回动画阶段的末尾,并且在animationDidStop例程被调用之前发生。有一个更好的方法吗?

编辑:将它纳入一个UIView动画作品的这种方式,您可以直接设置变换:

view.layer.transform = CATransform3DScale(CATransform3DIdentity, 0.001, 0.001, 0.001); 
view.layer.transform = CATransform3DRotate(view.layer.transform, M_PI, 1.0, 0.0, 0.0); 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.75]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
CATransform3D transform = CATransform3DRotate(view.layer.transform, M_PI, 1.0, 0, 0); 
transform = CATransform3DScale(rotationTransform, 1000, 1000, 1000); 
transform.m34 = 1.0/-500; 
view.layer.transform = transform; 
[UIView commitAnimations]; 

不过,我还是想回答我的原始查询,至于如何达到同样的成功地使用CAAnimation,因为它为动画提供了更大的灵活性。

编辑2:它似乎是我原来的问题(如何解决与CAAnimation问题)的答案实际上是非常简单的。为了保持最终状态(和消除闪烁),我只需要添加以下行:

anim.fillMode = kCAFillModeForwards; 
+0

不,它设置为YES层上进行动画。 – bellissimo 2012-02-18 15:13:38

+0

不幸的是,它没有任何区别。 – bellissimo 2012-02-18 15:30:42

回答

0

这可以帮助你:

animationView.layer.anchorPoint = CGPointMake(0.0f, 0.0f); 

    animationView.center = CGPointMake(0, 
            (animationView.center.y - 
            (animationView.bounds.size.height/2.0f))); 

    // start the Page Open 
    [UIView beginAnimations:@"Animation" context:nil]; 
    [UIView setAnimationDuration:2.0];  
    // set angle as per requirement 
    [animationView.layer setValue:[NSNumber numberWithInt:180] 
        forKeyPath:@"transform.rotation.x"]; 

    [UIView commitAnimations]; 
+0

谢谢,但我不认为达到了我所追求的目标。我知道在动画完成时会保留转换,但不允许缩放,或者最重要的是您使用CATransform3D获得的视角。 – bellissimo 2012-02-18 13:46:45

+0

好的,我将整个转换放置在UIView动画中,'view.layer.transform = transform;',这就实现了。感谢指针。我以为你不得不使用CAAnimation来做层动画,但似乎我错了。 – bellissimo 2012-02-18 17:11:49

+0

但是,我仍然有兴趣知道如何解决使用CABasicAnimation问题:) – bellissimo 2012-02-18 17:14:30

0

我知道这是不是一个干净和简单的答案你可能希望:)

但在这里(UIView Animation Tutorial: Practical Recipes )你可以找到一个可下载的例子,其中还显示了如何做规模&旋转。

如果您运行示例,您可以看到比例和旋转,单击HUD,然后单击停止。

+0

感谢您的链接,我喜欢'流失'动画,已纳入已经;)。它也给了一些其他有用的指针。 – bellissimo 2012-02-18 17:13:26