旋转 UIView iPhone [英] Rotate UIView iPhone
问题描述
我正在开发一款益智游戏,我需要在每次双击时将拼图旋转到 90
度角.
I am working on a puzzle game and I need to rotate the puzzle pieces to 90
degrees angle on every double tap.
我尝试使用两种不同的方式来做.第一种方法是这个:
I have tried to do did using two different ways. First method is this one:
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(90);
[UIView commitAnimations];
唯一的问题是这件作品没有旋转 90
度;它旋转了大约 100
度,并且这个动画正在修改拼图的框架 UIView
.
The only problems with that are that the piece is NOT rotating 90
degrees; it is rotating by around 100
degrees, and that this animation is modifying the frame of the puzzle piece UIView
.
这是我的控制台输出:
frame BEFORE ROTATION: {{35, 178}, {80, 80}}
frame AFTER ROTATION: {{21.3172, 164.317}, {107.366, 107.366}}
我尝试的第二种方法是这个:
The second method I tried was this one:
CABasicAnimation* spinAnimation = [CABasicAnimation
animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:1];
[self.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
这个方法的问题在于,在完成旋转后,它会将我的 UIView
反转到之前的状态.
The problem with this method is that after finishing the rotation, it is reversing my UIView
to its previous state.
如何在没有这些问题的情况下旋转带有动画的拼图?
How can I rotate the puzzle piece with an animation without having these problems?
推荐答案
尝试
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DegreesToRadians(90));
[UIView commitAnimations];
您可以在其中定义 DegreesToRadians 为
where you can define DegreesToRadians as
#define DegreesToRadians(x) ((x) * M_PI / 180.0)
这篇关于旋转 UIView iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!