如何在单个animation中缩放和旋转视图

我试图通过从屏幕的中心出现一个视图,同时将其增大到全尺寸,同时以3D方式围绕X轴旋转。 当我创build视图时,我会对其应用一个转换,以确保它缩小并旋转,从而开始(实际上不可见),然后尝试使用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"]; 

然而这个animation不会改变图层的实际转换,所以我也这样做:

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

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

编辑:将它合并到一个UIViewanimation作为这种方式,你可以直接设置变换:

 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,因为这为animation提供了更大的灵活性。

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

 anim.fillMode = kCAFillModeForwards; 

这可能会帮助你:

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

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

但在这里( UIViewanimation教程:实用食谱 ),你可以find一个可下载的例子,这也显示了如何做规模和旋转。

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

看到这个线程关于如何iPad应用程序商店的旋转和缩放的答案和代码: 如何翻转和放大UIView它同时像iOS 7的iPad应用程序商店?