Understanding convertPoint:toView: - 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

UIView convertPoint:toView for embedded views [duplicate]

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

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.

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.

What does setting CALayer's bounds.origin do?

In CALayer's API, 'position' is used for setting the position of the layer.
By my own testing, setting bounds.origin does not do anything. Am I missing something?
The bounds.origin controls where the origin of the layer's coordinate system is, relative to the layer's frame in its superlayer. Changing it has two visible effects:
The position of sublayers of the layer. For example, when you scroll a UIScrollView, the scroll view doesn't change its subview's frames. It simply changes its bounds.origin. I suggest setting up a toy app with a scroll view and doing NSLog("scroll view bounds = %#", NSStringFromCGRect(scrollView.bounds)); from a timer or some other trigger to get a sense of what's happening.
The origin of the graphics context coordinate system in drawInContext:. Mostly commonly you would see this effect in a view's drawRect: method. Your CGContext inside drawRect: will have been translated by the self.bounds.origin.
You may find it helpful to read about “View Geometry and Coordinate Systems” in the View Programming Guide for iOS and “Layer Objects Define Their Own Geometry” in the Core Animation Programming Guide, although really neither of them have a good discussion of the bounds origin.
Changing the bounds rectangle changes the position and size of the content in the coordinate system of the layer itself. Changing the frame (or position) changes the position of the layer in the coordinate system of its super layer. Usually you only want to change the frame, not the bounds.

Resources