旋转的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
到previous状态。
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屋!