3
votes

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.

3
If you find any of the answers useful you should accept one of them...that's the etiquette :)Moonwalker
Haven't had time time yet to try the answers out. If I find a helpful answer im going to accept them.Lukas Köhl

3 Answers

2
votes

Just get the frame of the picture:

picture.frame.origin.x
picture.frame.origin.y
2
votes

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

0
votes

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))