开发者

UIView's position

开发者 https://www.devze.com 2023-02-19 00:05 出处:网络
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 re开发者_开发问答lative to 320*480

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 re开发者_开发问答lative to 320*480)? I want to draw and fill a polygon using the position of views.


You can use the frame property of the UIView to retrieve its location and size. See the class reference for more information:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

Example:

... = myView.frame.origin.x; //x-coord
... = myView.frame.origin.y; //y-coord
... = myView.frame.size.width; //width
... = myView.frame.size.height; //height


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.


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,

  1. Are you drawing the Circles in the view using CoreGraphics?
  2. When you say 4 different views, how they are managed? Are they displayed at the same time?
  3. Are there 4 View objects added on 1 ViewController?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消