Frame, bounds represents the size and position of attributes and center, position, Anchorposition

Source: Internet
Author: User

In iOS development and development process often use the interface elements of the frame, bounds represents the size and position of the properties and center, position, Anchorposition, and so on a purely positional properties. What exactly do these attributes mean? What's the relationship between them? Here is a brief discussion.

First of all, frame and bounds.

Frame: Describes the position and size of the current interface element in its parent interface element.
Bounds: Describes the position and size of the current interface element in its own coordinate system.
iOS takes the upper-left corner as the coordinate origin (0,0), the origin to the right of the x-axis positive direction, and the lower side of the origin as the y-axis positive direction. iOS uses Cgpoint to represent the point in the coordinate system x, y position. At the same time, iOS uses Cgsize to represent the width and height of the interface elements, that is, the size of the view. CGRect, in combination with Cgpoint and cgsize, is used to represent the position and size of the rectangle. Its origin represents the position (cgpoint) of the upper-right corner of the rectangle, which represents the size of the rectangle (cgsize).
The size described by frame is consistent with the size described by bounds, which refers to the width and height of the interface elements. However, they describe a different location, where the frame describes the position of the upper-left corner of the interface element in the parent interface element coordinate system, and the position described by bounds refers to the position of the upper-left corner of the interface element in its own coordinate system, so the position described by bounds is always (0,0). It can be said that the biggest difference between frame and bounds is that they choose different coordinate systems. Such as:

Now, let's say the center, position, and anchorposition three properties that simply represent the location.

Say these three attributes before you have to understand the next calayer.
UIView is the basis for interface elements in iOS systems, and all interface elements are inherited from it. It is entirely by coreanimation to achieve it. The real drawing part of it is managed by a Calayer class. UIView itself is more like a Calayer manager, accessing its properties related to drawing and coordinates, such as Frame,bounds, is actually accessing the related properties of the Calayer it contains.
Center: Describes the position of the central point of the current interface element in its parent interface element. The center can only be accessed through the interface element itself, and Calayer does not provide this property for external access, but it is known from the previous description that the center property of the access interface element must also have been computed by invoking the related properties of Calayer. In general, we can derive the center from the calculation below.

[HTML]View PlainCopy
    1. Center.x = frame.origin.x + frame.size.width * 0.5;
    2. Center.y = FRAME.ORIGIN.Y + frame.size.height * 0.5;


But usually we don't use that kind of calculation.
Next, focus on the other two attributes position and anchorposition that describe the location.

[OBJC]View PlainCopy
    1. @property Cgpoint Position
    2. @property Cgpoint Anchorpoint


By declaring that we can find that they are all cgpoint types. Now we think of Calayer as a sheet of paper pinned to the table by a Pushpin (the equivalent of the parent interface element's Calayer), and you can imagine that this paper can rotate around this pushpin. Then the position of the Pushpin relative to the paper can be described by anchorposition, and the position of the pin on the table is position.
In iOS, the value of the Anchorpoint point is determined by a relative bounds scale value, in the upper-left corner of the printer, the lower-right corner, Anchorpoint (0,0), (1, 1), which means that anchorpoint is in the cell coordinate space ( is also defined in the left-hand coordinate system). Similarly, it can be concluded that the center point of the paper, the lower left and upper right corner of the Anchorpoint is (0.5,0.5), (0,1), (1,0). Please refer to the figure of iOS and MacOS, because the coordinate system is different, iOS uses the left-handed coordinate system, the coordinates origin in the upper left corner, MacOS uses the right-hand coordinate system, the origin point in the lower left corner, we look at the iOS section.

Take a look at the original definition of position: the layer's position in its superlayer ' s coordinate space. Chinese can be understood as position is the position of the layer relative to the superlayer coordinate space, it is clear that the position here is based on the anchorpoint to determine. As for how to determine, see the formula below.

[OBJC]View PlainCopy
    1. position.x = frame.origin.x + anchorpoint .x * bounds.size .WIDTH;    
    2. position.y = frame.origin.y + anchorpoint.y * bounds .size.HEIGHT;  


If you modify the position position of layer unilaterally, what effect will it have on anchorpoint? How does modifying anchorpoint affect position?
Although position is based on anchorposition to determine, but according to the code test, the two do not affect each other, the affected will only be frame.origin, that is, the layer coordinate origin relative to the superlayer will change. In other words, Frame.origin is determined by position and anchorpoint, and the formula above can be changed to the following.

[OBJC]View PlainCopy
    1. frame.origin .x = position.x - anchorpoint< Span class= "xcodeconstants" >.x * bounds.size .WIDTH;&NBSP;&NBSP;&NBSP;&NBSP;
    2. frame.origin.y = position.y - anchorpoint.y * bounds .size.HEIGHT;&NBSP;&NBSP;


This explains why modifying Anchorpoint moves the layer, because position is not affected, only frame.origin makes the corresponding change, and thus moves the layer.
Another description in Apple doc is this:

When you specify the frame of a layer, position are set relative to the anchor point. When you specify the position of the layer, bounds are set relative to the anchor point.

When you set the frame property of a layer, the position of the position point (that is, the position coordinate) is determined by the value of the anchor point (Anchorpoint), and when you set the Position property of the layer, The position of the bounds (that is, the orgin coordinate of the frame) is determined by the anchor Point (Anchorpoint).
If we need to modify the Anchorpoint, but do not want to move the layer and do not want to modify the Frame.origin, then according to the previous formula, you need position to do the corresponding modification. Simply deduced, the following formula can be obtained.

[OBJC]View PlainCopy
    1. Positionnew. x = Positionold. x + (Anchorpointnew. X-anchorpointold. x) * bounds. Size. Width
    2. Positionnew. y = positionold. Y + (anchorpointnew. Y-anchorpointold. Y) * bounds. Size. Height /c12>


There's really no need to be so troublesome. Modify anchorpoint and do not want to move the layer, after modifying the Anchorpoint and then re-set the frame can achieve the goal, then position will automatically change accordingly. Write the function as follows.

[OBJC]View PlainCopy
    1. -(void) Setanchorpoint: (cgpoint) Anchorpoint forview: (UIView *) view{
    2. CGRect oldframe = view. Frame;
    3. View. Layer. Anchorpoint = Anchorpoint;
    4. View. frame = Oldframe;
    5. }


The above content is I combined with the development experience and reference to some of the information and written, if there is inappropriate also please enlighten us, we would like to learn from each other and make progress together.

Reprinted from: http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/e20914053/article/details/49950307

Frame, bounds represents the size and position of attributes and center, position, Anchorposition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.