An explanation of Anchorpoint and position in iOS development
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="font-size: 18px; color: #ff0000;">(this article is reproduced, thank the original author described very Accurate)</span></strong></p></p><p><p>Introduction</p></p><p><p>Believe that the first contact to Calayer people will encounter the following questions: why modify the Anchorpoint will move the location of the layer? What is the point of Calayer's position? What is the relationship between Anchorpoint and position?</p></p><p><p>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. What is position and anchorpoint? First look at the two prototypes, it is Cgpoint point.</p></p><p><p>@property Cgpoint position;</p></p><p><p>@property Cgpoint anchorpoint;</p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 24px; color: #4d6df3;"><strong>Anchorpoint</strong></span></p></p><p><p>Generally are introduced position first, and then introduce Anchorpoint. Here in turn, say Anchorpoint First.</p></p><p><p>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.</p></p><p><p>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, the Anchorpoint is divided into (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 white paper, the lower left corner and the upper right corner of the anchorpoint is (0.5,0.5), (0,1), (1,0).</p></p><p><p></p></p><p><p>And then look at the following two pictures:</p></p><p><p></p></p><p><p>1. Anchorpoint = Cgpointmake (0.5, 0.5)</p></p><p><p></p></p><p><p>2. Anchorpoint = Cgpointmake (0.0, 0.0);</p></p><p><p></p></p><p><p>Then the result of rotating the same angle is performed:</p></p><p><p></p></p><p><p></p></p><p><p><br></p></p><p><p></p></p><p><p></p></p><p><p><strong><span style="font-size: 24px; color: #4d6df3;">Position</span></strong></p></p><p><p>To be exact, position is the position coordinate 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.</p></p><p><p>Then look at the original definition of position: the layer ' sposition in its superlayer ' 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.</p></p><p><p></p></p><p><p></p></p><p><p><strong><span style="font-size: 24px; color: #4d6df3;">anchorpoint, position, Frame</span></strong></p></p><p><p>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:</p></p><p><p><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">position.x = frame.origin.x + 0.5 * bounds.size.width;<span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">POSITION.Y = frame.origin.y + 0.5 * bounds.size.height;</span></span></p></p><p><p>The 0.5 inside is because Anchorpoint takes the default value, and the more general formula should be the Following:</p></p><p><p><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">position.x = frame.origin.x + anchorpoint.x *bounds.size.width; <span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">position.y = frame.origin.y + anchorPoint.y *BOUNDS.S ize.height;</span></span></p></p><p><p></p></p><p><p><strong><span style="font-size: 32px; color: #4d6df3;">Special attention</span></strong></p></p><p><p>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?</p></p><p><p>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:</p></p><p><p><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">frame.origin.x = Position.x-anchorpoint.x *bounds.size.width; <span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">frame.origin.y = Position.y-anchorpoint.y *BOUNDS.S ize.height;</span></span></p></p><p><p>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.</p></p><p><p></p></p><p><p><strong><span style="font-size: 24px; color: #4d6df3;">Understanding and Application</span></strong></p></p><p><p>Because position points and anchorpoint points are independent, they do not change because of another change, but in the actual situation, there are often such a demand, I need to modify the anchorpoint, But do not want to move the layer is not to modify 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:</p></p><p><p><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;"><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">positionnew.x= positionold.x + (anchorpointnew.x-anchorpointold.x) *bounds.size.width <span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">positionNew.y = Positionold.y + (anchorpointnew.y-anchorpointold.y) * bounds.size.height</span></span></span></p></p><p><p><span style="color: #4d6df3;"> <span style="text-decoration: underline;">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:</span></span></p></p><p><p><span style="color: #4d6df3;"><span style="font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">-(<span style="font-weight: bold;">void) setanchorpoint: (cgpoint) anchorpoint forview: (UIView *) view{ <span style="font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">cgrect oldframe= view; <span style="font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">View.layer.anchorPoint = anchorpoint; <span style="font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">View.frame = oldframe; <span style="font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">}</span></span></span></span></span></span></span></p></p><p><p></p></p><p><p>Summarize</p></p><p><p>1, position is the position coordinates of the anchorpoint in the layer in the Superlayer.</p></p><p><p>2, the principle of non-impact: individually modify position and anchorpoint any one of the properties does not affect another Property.</p></p><p><p>3, frame, position and Anchorpoint have the following relationship:</p></p><p><p><span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">frame.origin.x = Position.x-anchorpoint.x *bounds.size.width; <span style="color: #333333; font-family: Monaco, Menlo, Consolas, ‘Courier new‘, monospace; font-size: 12px; line-height: 18px; white-space: pre-wrap;">frame.origin.y = Position.y-anchorpoint.y *BOUNDS.S ize.height;</span></span></p></p><p><p>An explanation of Anchorpoint and position in iOS development</p></p></span>
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