# move a UIImageView "up"

Discussion in 'iOS Programming' started by Tex-Twil, Jul 27, 2011.

1. Jul 27, 2011
Last edited: Jul 27, 2011

### Tex-Twil macrumors 68020

Joined:
May 28, 2008
Location:
Europe
#1
Hi,
I have a UIImageView representing the level of the "artificial horizon" in a plane. Blue is the sky, brown is the ground. This image is actually clipped by its super view which creates the square.

When I I move the nose of the plane up, I move the image down:

When I move the nose of the plane down, I move the picture up :

This is called the "pitch"

So far I've been modifying only the x axis of the image:

Code:
```imageHorizon.center = CGPointMake(imageHorizon.center.x, imageHorizon.center.y + valueToMove);
```
When I "roll" the plane I rotate the picture either to the left or right:

Code:
```imageHorizon.transform = CGAffineTransformRotate(imageHorizon.transform, angleToRotate*(CGFloat)(M_PI/180));
```

Now, the problem is that once I rotated the picture and want to modify the "pitch", I dont know which formula to use to find the correct x,y to move the picture towards the sky or ground direction:

In other words, when I rotate the picture with an angle β, what are the x, y value to add to the center of the image to move it on the red line ?

Tex

2. ### ChOas macrumors regular

Joined:
Nov 24, 2006
Location:
The Netherlands
#2
3. Jul 27, 2011
Last edited: Jul 27, 2011

### Tex-Twil thread starter macrumors 68020

Joined:
May 28, 2008
Location:
Europe
#3
ok I will have a look but it seems quite complex. I thought that using simple trigonometry should work:

cos(α) = x /h
sin(α) = y / h

so
x = h * cos(α)
y = h * sin(α)

but anyway in my code it's not working The image is not moving to the right direction:

Code:
```// derive from beta and convert to radians
CGFloat alpha = (90 - beta)  * (CGFloat) (M_PI/ 180);
CGFloat moveX = distanceToMove * cos(alpha);
CGFloat moveY = distanceToMove * sin(alpha);
imageHorizon.center = CGPointMake(imageHorizon.center.x + moveX, imageHorizon.center.y + moveY);
```

4. Jul 29, 2011
Last edited: Jul 29, 2011

### Sykte macrumors regular

Joined:
Aug 26, 2010
#4
If I'm not mistaken you want to keep it in degrees and not radians. Then + or - your current location.

5. ### Tex-Twil thread starter macrumors 68020

Joined:
May 28, 2008
Location:
Europe
#5
I got it working with a much easier solution. I put the image in a UIView. I apply the rotation to this view. Then I apply the pitch to the image (which is subview of the "roll" view. Since it is a subview, the coordinates are relative to the superview meaning that I have to simply add or subtract a value the Y coordinate