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

时间:2012-02-18 12:54:01

标签: iphone ios core-animation rotation scale

我试图通过使其从屏幕中心出现,同时增长到其全尺寸,同时还以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成功实现相同的效果,因为这样可以为动画提供更大的灵活性。

Edit2:我的原始问题(如何解决CAAnimation问题)的答案似乎非常简单。为了保持结束状态(并消除闪烁),我只需要添加以下行:

anim.fillMode = kCAFillModeForwards;

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

我知道这不是一个你可能希望的简洁明了的答案:)

但是这里(UIView Animation Tutorial: Practical Recipes 你可以找到一个可下载的例子,它也展示了如何进行规模和扩展。旋转。

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

答案 2 :(得分:0)

请参阅此主题,了解iPad应用商店如何轮换和缩放答案和代码:How can I flip and enlarge a UIView it at the same time like iOS 7 iPad App Store?