1

我有一个 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);

}];

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

4

1 回答 1

0

是的,您所做的是您将其旋转 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  


希望对你有帮助:)

于 2013-08-13T04:44:38.233 回答