【问题标题】:Animating rotated UIView causes it to disappear动画旋转的 UIView 会导致它消失
【发布时间】:2013-08-12 21:03:08
【问题描述】:

我有一个 10 x 10 的 UIView,旋转 45 度以创建菱形。

这是旋转视图的代码:

triangle = [[UIView alloc] initWithFrame:CGRectMake(35, kUserViewHeight - 5, 10, 10)];
triangle.backgroundColor = [UIColor coolBlue];
//triangle.center = CGPointMake(5.0, 5.0);
triangle.transform = CGAffineTransformMakeRotation(M_PI_2 / 2);

我特意注释掉了中心值,因为这会弄乱设置视图的位置。我完全意识到这可能是问题所在。

视图完美显示,但是当我制作动画时,视图会移动到它应该移动的位置,但在途中,它会消失。当我摆脱 triangle.transform 属性时,视图动画完美且不会消失,但显然它只是一个正方形而不是菱形。

这里是动画代码:

[UIView animateWithDuration:kAnimationInterval animations:^{

        triangle.frame = CGRectMake(115, kUserViewHeight - 5, 10, 10);

}];

因为我打算四处移动菱形,所以设置中心变得有问题。我怎样才能既旋转视图,然后在屏幕上为其设置动画而不会消失?是否有某种特殊的方法可以将中心点计算为我需要的位置?谢谢!

【问题讨论】:

    标签: ios animation uiview transform


    【解决方案1】:

    是的,你所做的是将它旋转 90 度,正如你所说它正在消失,这样做可能对你有帮助 当你为订单制作动画时有一件事很重要


     UIView* triangle = [[UIView alloc] initWithFrame:CGRectMake(35, 25, 10, 10)];
     triangle.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1]; // i am seeting diff color but not matter
     [UIView animateWithDuration:0.2 animations:^{
      triangle.frame = CGRectMake(115, 20, 10, 10);//kUserViewHeight = 25 - 5 (i am assuming)        
     //it must be after setting the frame, now put rotation for 45 degree
    
     triangle.transform = CGAffineTransformMakeRotation(M_PI/4);//it is M_PI/4 not M_PI /2
    
    }];
    
    
     //  triangle.transform = CGAffineTransformMakeRotation(M_PI/4); //you can put it hear or within the animation block  
    


    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多