Lukas Köhl
Lukas Köhl

Reputation: 1579

Get the position of a UIImageView

I have an existing UIImageView with variable X and Y coordinates. The picture should move according to the current position difference but I don't know how I get the current position.

picture = UIImageView(frame: CGRectMake(corx, cory, 25, 25))

I hope you can help me out.

Upvotes: 3

Views: 10000

Answers (3)

Moonwalker
Moonwalker

Reputation: 3802

You can get coordinates of a view in two different ways.

picture.bounds - The bounds rectangle, which describes the view’s location and size in its own coordinate system.

picture.frame - The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.

in both cases the size component should be the same.

In the code below origin will be the point with your original coordinates (corx,cory).

CGPoint origin = picture.frame.origin

You probably want to use picture.frame.origin to animate your view. You can always say picture.frame = <new frame> once you have created your instance and adjust this new frame's origin.

Example

CGRect r = picture.frame;
r.origin.x += 10;
r.origin.y += 10;
picture.frame = r;

This will not be a smooth animation though - you can animate frame property using CoreAnimation for a smooth transition.

Hope this helps

Upvotes: 2

keithbhunter
keithbhunter

Reputation: 12324

Use the x and y coordinates from the frame of the view you want to base it off of. Anything inheriting from UIView will have a frame associated with it.

var picture = UIImageView(frame: CGRectMake(view.frame.origin.x, view.frame.origin.y, 25, 25))

Upvotes: 0

Choppin Broccoli
Choppin Broccoli

Reputation: 3066

Just get the frame of the picture:

picture.frame.origin.x
picture.frame.origin.y

Upvotes: 2

Related Questions