1

我正在使用加速度计在屏幕上移动图像(直升机)。我正在尝试添加代码,因此当直升机向前移动时,它会将 uiimageview 旋转 20 度,当直升机向后移动时,它会将 uiimageview 旋转 -20 度,其想法是创建直升机移动更逼真的效果。但是,当应用旋转时,uiimageview 会移回其原始起始位置(我将其放置在故事 bard 中的位置)。任何想法为什么?这是代码:

-(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;

}

4

1 回答 1

0

你试过这个吗?要保留现有的变换并在其上应用旋转:

   helicopter.transform = CGAffineTransformRotate([helicopter transform],angle ); 

然后回到水平,您可能必须应用负角:

   helicopter.transform = CGAffineTransformRotate([helicopter transform], -angle ); 
于 2014-04-11T09:25:26.083 回答