我正在使用加速度计在屏幕周围移动图像(直升机)。我正在尝试添加代码,因此当直升机向前移动时,它将uiimageview旋转20度,当直升机向后移动时,它将uiimageview旋转-20度,这个想法是创造直升机移动更逼真的效果。但是,当应用旋转时,uiimageview将移回其原始起始位置(我将其置于故事中的位置)。有什么想法吗?这是代码:
-(void)outputAccelerationData:(CMAcceleration)acceleration
{
if (helicoptervalueX>1.5){
helicopterrotation = 1;
helicopter.transform = CGAffineTransformRotate(CGAffineTransformIdentity,0.34906585 );
NSLog(@"Tilt forward");
} else if (helicoptervalueX < -1.5){
helicopterrotation = -1;
helicopter.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -0.34906585);
NSLog(@"Tilt backward");}
else if (helicoptervalueX<=1.5 && helicoptervalueX>=-1.5){
helicopterrotation = 0;
helicopter.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 0);
NSLog(@"Helicopter flat");}
helicoptervalueX = -(acceleration.y*50.0)+calibrationx;
helicoptervalueY = (acceleration.x*50.0)+calibrationy;
int helicopternewX = (int)(helicopter.center.x + helicoptervalueX);
if (helicopternewX > 518) {
helicopternewX = 518;}
if (helicopternewX < 50){
helicopternewX = 50;}
int helicopternewY = (int)(helicopter.center.y - helicoptervalueY);
if (helicopternewY > 295){
helicopternewY = 295;}
if (helicopternewY < 25){
helicopternewY = 25;}
CGPoint helicopternewCenter = CGPointMake(helicopternewX, helicopternewY);
helicopter.center = helicopternewCenter;
}
答案 0 :(得分:0)
helicopter.transform = CGAffineTransformRotate([helicopter transform],angle );
然后回到水平,您可能需要应用负角度:
helicopter.transform = CGAffineTransformRotate([helicopter transform], -angle );