I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view relative to the window(i mean relative to 320*480)? I want to draw and fill a polygon using the position of views.
5 Answers
You can use the frame
property of the UIView
to retrieve its location and size. See the class reference for more information:
Example:
... = myView.frame.origin.x; //x-coord
... = myView.frame.origin.y; //y-coord
... = myView.frame.size.width; //width
... = myView.frame.size.height; //height

- 54,662
- 15
- 117
- 144
-
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInView: viewController.view];//Getting the Touch Coordinate // NSLog(@"%f%f",touchLocation.x,touchLocation.y); if((touchLocation.x<350)&& (touchLocation.y<440)) { CGRect rect= CGRectMake((touchLocation.x)-5, (touchLocation.y)-25, 7, 7); self.frame=rect; [self setNeedsDisplayInRect:rect]; } CGPoint a= viewController.view1.center ; NSLog(@"%f",a); } – Priya Mar 24 '11 at 06:27
-
@priya: please post the code in your question so that we can understand it a lot better... Please use the "display code" option in your question section – A for Alpha Mar 24 '11 at 07:03
You can grab the position in the following way:
CGPoint positionOfAView = view.frame.origin;
or if transforms were applied:
CGPoint positionOFAView = view.bounds.origin;
Alternatively, you may want to grab the center:
CGPoint centerOfAView = view.center;
See this answer, too.

- 1
- 1

- 9,529
- 9
- 60
- 111
You have posted the same question again.(your first question) Before anybody answer's this question the same way it was answered earlier. Can you please tell,
- Are you drawing the Circles in the view using CoreGraphics?
- When you say 4 different views, how they are managed? Are they displayed at the same time?
- Are there 4 View objects added on 1 ViewController?

- 1
- 1

- 2,344
- 3
- 18
- 27
-
I am doing a viewbased application and i added a subclass of UIView as DrawCircle and inside the drawrect i have given the code for drawing circle.In the nib file i have given an image and added 4 views.Initially all the views will appear with a circle because i made the views as subclass of DrawCircle.Now the user can drag the circles to anywhere.But i want to fill the area enclosed by the views with a color. – Priya Mar 23 '11 at 12:38
-
Still I am confused. You are able to move the views right on the touch events, right? And you want to fill the area enclosed by views(do you mean all four views?) with some color. do you mean only the background area of the views should be colored? – Vaibhav Tekam Mar 24 '11 at 06:14
-
-
well there can be two approaches... 1. you set backgroundColor to your DrawCircle Class. This way, by default all the views will be having background color. or else,2. to calculate the frame occupied by all the views, you will need to get the frame of all the views using ".frame" property of your drawcircle objects. – Vaibhav Tekam Mar 24 '11 at 06:59
-
I just want to know the position of each view relative to the window..can u please send me a sample code for it. – Priya Mar 24 '11 at 07:03
-
-
In your ViewController, create IBOutlets for the four views you added, make the connection and then using frame property of the IBOutlet, you can get the position of them on the view. – Vaibhav Tekam Mar 24 '11 at 07:38
You can get the position using
CGRect frame = [myView frame];
But remember it will send coordinates and origin based on its parent view
And after making changes
myView.frame = frame;
Hope this helps....

- 1,249
- 2
- 20
- 38
-
CGRect frame = [myView frame];..This will return what? I want to get x and y coordinates of view – Priya Mar 24 '11 at 04:37
-
@Priya: frame contains origin and size. They can be used like frame.origin.x, frame.origin.y and frame.size.width and frame.size.height. Change these values as you like and set the new frame to he view. :) – SNR Mar 24 '11 at 06:09
If you want to just move a view (without changing it size), consider using the center property, it might be more convenient.
Here's an example, assuming your circle structure is built in a certain way :)
myView.center = CGPointMake(circle.x + circle.width/2, circle.y + circle.height/2);

- 14,324
- 4
- 25
- 31