UIView convertPoint:toView for embedded views [duplicate] - ios

I do not quite understand the method convertPoint:toView:.
In Apple's documentation it is written that
convertPoint:toView:
Converts a point from the receiver’s coordinate system to that of the
specified view.
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
But what does converting a point from one to the other actually mean?
Does it imply that the points in both bounds have different units? Or just different values?
If it is the latter, why is there such a method when we can simply assign the value of a's contentOffset to b's ?
CGPoint a = [a contentOffset];
[b setContentOffset:a];
How is convertPoint:toView: different from simply assigning contentOffset? Or did I misunderstand the entire concept? What does converting points actually do? When should this method be used?

Every UIView has its own coordinates system. So if you have a UIView_1 that contains another UIView_2, they both have a point (10,10) within them.
convertPoint:toView: allows the developer to take a point in one view and convert the point to another view coordinate system.
Example:
view1 contains view2. The top left corner of view2 is located at view1 point (10,10), or better to say view2.frame.orgin = {10,10}. That {10,10} is based in the view1 coordinate system. So far so good.
The user touches the view2 at point {20,20} inside the view2. Now those coordinates are in the view2 coordinate system. You can now use covertPoint:toView: to convert {20,20} into the coordinate system of view1. touchPoint = {20,20}
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
So now pointInView1Coords should be {30,30} in the view1 coordinate systems. Now that was just simple math on this example, but there are all sorts of things that contribute to the conversion. Transforms and scaling come to mind.
Read about UIView frame, bounds, and center. They are all related and they deal with coordinate systems for a view. Its confusing until you start doing stuff with them. Remember this frame and center are in the parent's coordinate system. Bounds is in the view's coordinate system.
John

Related

what does view.center means in ios?

just as the question title,what does view.center means,
on my first look I think it's the center coordinate,but my practice proved it's not.
for example,I try to usesubView.center = parentView.center
to set subView locates in center of parentView,but not succeed,
so who can tell me the meaning of view.center,thanks!
The subView.center = parentView.center will not work because center of the superview is using its superview coordinates. See UIView Class Reference for more information.
A centre is a CGPoint expressed in terms of the superview's coordinate system and it determines the position of the exact center point of the view.
From Apple Documentation:
The center property can be used to adjust the position of the view
without changing its size. The bounds defines the internal dimensions
of the view as it sees them and is used almost exclusively in custom
drawing code.

What is the relationship between a UIView's frame origin and its center?

Say I'm creating an animation, and I want to move the center of a UIView to a certain CGPoint I have. I know what I want the center to be, just the aforementioned CGPoint, and I know the width and height that I want, but when I go to create the frame for my UIView I have no idea what to set for the x and y for the origin.
Should I just be setting 0, 0 or anything really? Does it matter?
Should I just be thinking as setting center as a different method of setting the origin? Makes me wish CGRect had an instantiator with center as an option.
There has been a previous question addressing a somewhat similar question, but it addresses them as three separate entities, rather than how to deal with center when creating the view.
Yes, go ahead and set the initial frame's origin to anything, since subsequently setting the center will effectively move this origin anyway. It is not uncommon to initialise views with a frame of CGRectZero when it is going to be fully configured later.
frame is actually just a calculated property based on the view's center and bounds, and so all three are intrinsically linked when it comes to view layout. The UIView Class Reference has this to say about it:
The geometry of a view is defined by its frame, bounds, and center
properties. The frame defines the origin and dimensions of the view in
the coordinate system of its superview and is commonly used during
layout to adjust the size or position of the view. The center property
can be used to adjust the position of the view without changing its
size. The bounds defines the internal dimensions of the view as it
sees them and is used almost exclusively in custom drawing code. The
size portion of the frame and bounds rectangles are coupled together
so that changing the size of either rectangle updates the size of
both.

Rotate coordinate System of an UIView

Im a complete beginner, so please excuse my maybe stupid question.
Im using an UIView "superview" with many children-images to perform a rotation of the images around an arbitrary point:
superview.transform = CGAffineTransformMakeRotation(M_PI/2);
This works perfect, but it seems like this is rotating the coordinate system of the superview as well. This in turn messes up all following steps. I find hundreds of posts and infos concerning rotating images and for mac development there obviously is a method for rotating coordinate systems. I just don't find anything for iOS.
Is there a way to rotate a view without rotating the coord system / how can I rotate the coord system without influencing the images?
Thanks for any help.
it seems like this is rotating the coordinate system of the superview as well
You're right -- rotating the coordinate system of the superview is exactly what you're doing:
superview.transform = CGAffineTransformMakeRotation(M_PI/2);
Is there a way to rotate a view without rotating the coord system
Sure, just set the transform property for your view rather than for its superview. For any view someView, you can say:
someView.transform = CGAffineTransformMakeRotation(M_PI/2);
Note that this actually rotates the view's own coordinate system -- it doesn't rotate the view itself. That means that all your drawing will be rotated, but the view's frame will remain the same. You might need to adjust the frame to account for this. For example, if you have a rectangular view that's normally 100px high and 300px wide, you might want to make it 300px high and 100px wide to ensure that the content is all still visible.
If you're creating a custom view, you can of course refer to instances of that view from inside its own code using self:
self.transform = CGAffineTransformMakeRotation(M_PI/2);

Bounds vs Frame when making a custom view and initializing it

I'm having some difficulties understanding the difference, and when to use what.
I know the textbook definitions. I have also searched a lot regarding this very topic. Some answers here on SO were helpful to some extent, but I feel I still don't understand this properly.
Let's say I have a aCustomView.m, and when I place UI elements within that view, I use bounds, which makes sense because it's in it's own coordinate system, but, when I initialize the view using initWithFrame: in my view-controller, should I use self.view.frame or self.view.bounds? Both would work, but with different results.
I really want to understand this, so any help would be appreciated.
The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).
The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
So the difference is only a question of representation.

Understanding convertPoint:toView:

I do not quite understand the method convertPoint:toView:.
In Apple's documentation it is written that
convertPoint:toView:
Converts a point from the receiver’s coordinate system to that of the
specified view.
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
But what does converting a point from one to the other actually mean?
Does it imply that the points in both bounds have different units? Or just different values?
If it is the latter, why is there such a method when we can simply assign the value of a's contentOffset to b's ?
CGPoint a = [a contentOffset];
[b setContentOffset:a];
How is convertPoint:toView: different from simply assigning contentOffset? Or did I misunderstand the entire concept? What does converting points actually do? When should this method be used?
Every UIView has its own coordinates system. So if you have a UIView_1 that contains another UIView_2, they both have a point (10,10) within them.
convertPoint:toView: allows the developer to take a point in one view and convert the point to another view coordinate system.
Example:
view1 contains view2. The top left corner of view2 is located at view1 point (10,10), or better to say view2.frame.orgin = {10,10}. That {10,10} is based in the view1 coordinate system. So far so good.
The user touches the view2 at point {20,20} inside the view2. Now those coordinates are in the view2 coordinate system. You can now use covertPoint:toView: to convert {20,20} into the coordinate system of view1. touchPoint = {20,20}
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
So now pointInView1Coords should be {30,30} in the view1 coordinate systems. Now that was just simple math on this example, but there are all sorts of things that contribute to the conversion. Transforms and scaling come to mind.
Read about UIView frame, bounds, and center. They are all related and they deal with coordinate systems for a view. Its confusing until you start doing stuff with them. Remember this frame and center are in the parent's coordinate system. Bounds is in the view's coordinate system.
John

Resources