旋转UIView iPhone

And*_*csu 12 iphone animation rotation uiview

我正在开发一款益智游戏,我需要90在每次双击时将拼图旋转到角度.

我尝试用两种不同的方式做到了.第一种方法是这样的:

[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(90);
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

唯一的问题是这件作品不是旋转90度数; 它绕着大约旋转100,这个动画正在修改拼图的框架UIView.

这是我的控制台输出:

frame BEFORE ROTATION: {{35, 178}, {80, 80}}

frame AFTER ROTATION: {{21.3172, 164.317}, {107.366, 107.366}}
Run Code Online (Sandbox Code Playgroud)

我尝试的第二种方法就是这样:

CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:1];
[self.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
Run Code Online (Sandbox Code Playgroud)

这种方法的问题在于,在完成旋转之后,它将我的UIView状态转换为之前的状态.

如何在没有这些问题的情况下用动画旋转拼图?

vis*_*kh7 48

尝试

[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DegreesToRadians(90));
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

你可以在哪里定义DegreesToRadians

#define DegreesToRadians(x) ((x) * M_PI / 180.0)
Run Code Online (Sandbox Code Playgroud)


Lud*_*dry 5

你的第一种方法是好的,角度是弧度

[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(90/180*M_PI);
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)