对旋转的UIView设置动画会使其消失



我有一个UIView,它是10乘10,旋转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);
}];

因为我计划四处移动菱形,所以设置中心就成了问题。如何既旋转视图,又在屏幕上设置动画而不使其消失?有没有一种特殊的方法来计算中心点,使其准确地位于我需要的位置?谢谢

是的,你所做的是把它旋转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  


希望能帮助你:)

最新更新