Understanding position and anchorpoint[]

Source: Internet
Author: User

Introduction

Believe that the first contact to Calayer people will encounter the following problems:
Why does modifying anchorpoint move the layer's position?
What is the point of Calayer's position?
What is the relationship between Anchorpoint and position?

Each uiview is associated with a calayer, UIView has frame, bounds, and center three properties, and Calayer has similar attributes, frame, bounds, position, anchorpoint, respectively. Frame and bounds better understand,bounds can be regarded as x-coordinate and y-coordinate is 0 frame, that position, Anchorpoint is what? First look at the two prototypes, it is Cgpoint point.

@property Cgpoint Position
@property Cgpoint Anchorpoint

Anchorpoint

Start with an example, imagine, put a A4 of white paper with a Pushpin on the desk, if not very tight, the white can be rotated clockwise or counterclockwise around the Pushpin, when the Pushpins play the role of Fulcrum. We want to explain the anchorpoint is equivalent to the white pin, its main function is used as the pivot point of transformation , rotation is a transformation, similar to the translation, scaling.

Continue to expand, it is clear that the rotation of the white paper with the position of the pin is different, the Pushpin is set in the middle of the white paper and the upper left corner respectively created two rotation patterns, which is determined by the position of the Pushpin (Anchorpoint). How to measure the position of Pushpins (Anchorpoint) in white Paper? In iOS, the value of the Anchorpoint point is determined by a relative bounds scale value, in the upper-left corner of the white paper, in the lower- right corner, Anchorpoint is divided into (0,0), (1, 1), That is, anchorpoint is defined in the cell coordinate space (which is also the left-hand coordinate system). Similarly, it can be concluded that the center point of the White Paper, the lower left corner and the upper right corner of the Anchorpoint is (0.5,0.5), (0,1), (1,0).

Then take a look at the following two pictures, note that the image is divided into iOS and MacOS, because the coordinate system is different,iOS use the left-handed coordinate system, the coordinates origin in the upper left corner , MacOS using the right-hand coordinate system, the origin point in the lower left corner, we look at the iOS section.


Figure 1


Figure 2

Like UIView have Superview, and subview concept, Calayer also have superlayer and layer concept, the front of the white Paper and the diagram of the rectangle can be understood as a layer, A coordinate system other than the rectangle in the diagram can be understood as superlayer. If the upper-left corner is the origin, then there is a relative two coordinate space in the diagram.

position

In Figure 1, Anchorpoint has two cases (0.5,0.5) and (0,0), respectively, the center point of the rectangle and the origin point. So, what are the actual positions of the two anchorpoint in the Superlayer? A simple calculation can be obtained (100, 100) and (40, 60), comparing these two values with their respective position values, and finding the exact same, which is not a coincidence?

This time can be bold speculation, position is not anchorpoint in the Superlayer position? The answer is OK, or more specifically, theposition is the position coordinates of the anchorpoint point in the layer in Superlayer. Therefore, it can be said that the position point is relatively suerlayer, Anchorpoint point is a relative layer, the two are relatively different coordinate space of a coincident point.

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.

Figure 2 is the shape of the rectangle that rotates along the different anchorpoint points, which is similar to the two types of rotation that were created when the Pushpin was set in the middle of the white paper and the upper left corner.

Anchorpoint, Position, frame

The default value for Anchorpoint is (0.5,0.5), which is the anchorpoint default at the center point of the layer. By default, when you add a layer using the Addsublayer function, if you know the frame value of the layer, according to the above conclusion, the position value can be calculated using the following formula:

0.5 * bounds.size.width;  0.5 * bounds.size.height;

The 0.5 inside is because Anchorpoint takes the default value, and the more general formula should be the following:

position.x = frame.origin.x + anchorpoint.x * bounds.size.width;  = frame.origin.y + anchorpoint.y * BOUNDS.S Ize.height;

Here are two other questions, if unilaterally modify the position position of layer, what will be the impact on Anchorpoint? How does modifying Anchorpoint affect position ?
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:

frame.origin.x = Position.x-anchorpoint.x * bounds.size.width;  frame.origin.y = Position.y-anchorpoint.y * bounds. Size.Height;

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.

Understanding and Application

In Apple Doc's description of frame, there is this sentence:

Layers has an implicit frame, which is a function of the position, bounds, anchorpoint, and transform properties.

We can see that the formula we derive is basically in line with this paragraph description, but also lacks the transform, adds the transform the words to be more complex, here does not unfold to speak.

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 is determined by the value of the anchor (Anchorpoint), and when you set the Position property of the layer, bounds is determined by the anchor Point (Anchorpoint).

The first half of this translation is easy to understand according to the previous formula, the latter half of the sentence may be a bit confusing, when modifying the position, bounds width and height will be modified with it? In fact, position is the point, bounds is the rectangle, according to the anchor Point (anchorpoint) to determine only their position, not the internal properties. Therefore, the above paragraph of English translation is easy to understand:

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

In the actual situation, there may be a need, I need to modify the Anchorpoint, but do not want to move the layer is not to modify the Frame.origin, then according to the previous formula, you need to position to do the corresponding modification . Simply deduced, you can get the following formula:

positionnew.x = positionold.x + (anchorpointnew.x-anchorpointold.x)  * bounds.size.width  positionnew.y = Positionold.y + (ANCHORPOINTNEW.Y-ANCHORPOINTOLD.Y)  * bounds.size.height

But there is no need to be so troublesome in actual use. 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. The function is written as follows:

-(void) Setanchorpoint: (cgpoint) anchorpoint Forview: (UIView *) view{  cgrect oldframe = view.frame;  View.layer.anchorPoint = Anchorpoint;  View.frame = Oldframe;}
Summary

1, position is the position coordinates of the anchorpoint in the layer in the Superlayer.
2, the principle of non-impact: Individually modify position and anchorpoint any one of the properties does not affect another property.
3, frame, position and Anchorpoint have the following relationship:

frame.origin.x = position.x-anchorpoint.x * bounds.size.width;  = Position.y-anchorpoint.y * bounds.size . Height;

2nd of the principle of non-impact can also be understood: position and anchorpoint are in different coordinate space in the coincident point, modify the location of the coincident point in one coordinate space does not affect the location of the coincident point in another coordinate space.

Understanding position and anchorpoint[]

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.