Layer geometry
Don't come here if you're not familiar with geometry --The signature of the entrance to Plato's College.
In Chapter Two, we describe the image behind the layer, and some properties that control the coordinates and rotation of the layer. In this chapter, we'll look at how the inside of the layer controls position and size based on the parent layer and the sibling layer. We will also cover how to manage the geometry of the layer and how it is affected by auto-adjustment and automatic layout.
Layout
UIView has three more important layout properties: Frame,bounds and Center,calayer are referred to as frame,bounds and position. To be clear, the layer uses "position" and the View uses "center", but they all represent the same value.
Frame represents the outer coordinates of the layer (that is, the space occupied on the parent layer), bounds is the internal coordinates ({0, 0} is usually the upper-left corner of the layer), and center and position represent the location relative to the parent layer anchorpoint. The properties of the Anchorpoint will be introduced later, and it is now good to think of it as the center point of the layer. Figure 3.1 shows how these properties depend on each other.
Figure 3.1 Coordinate systems of UIView and Calayer
The Frame,bounds and Center properties of a view are just access methods , and when the frame that manipulates the view is actually changing the frame that is calayer below the view, it is not possible to change the view frame independently of the layer.
For a view or a layer, frame is not a very clear attribute, it is actually a virtual property, calculated based on bounds,position and transform, so when any of these values change, the frame will change. Conversely, changing the value of a frame also affects the values in them.
Remember that when you transform a layer, such as rotate or zoom, the frame actually represents a rectangular area that spans the entire axis aligned after the layer rotates, meaning that frame's width and height may no longer be the same as the width of the bounds (Figure 3.2).
Figure 3.2 Rotate the frame property after a view or layer
Anchor Point
As mentioned previously, both the center property of the view and the position property of the layer specify the position of the anchorpoint relative to the parent layer. The anchorpoint of a layer controls the position of its frame by position, and you can think of Anchorpoint as a handle to move the layer.
By default, Anchorpoint is positioned at the midpoint of the layer, so the layer will be centered at this point. The Anchorpoint property is not exposed by the UIView interface, which is why the position property of the view is called "center". But the layer's anchorpoint can be moved, for example, you can put it in the upper-left corner of the layer frame, so the contents of the layer will move in the direction of the lower-right corner of the position (Figure 3.3), not centered.
Figure 3.3 Effect of changing anchorpoint
Similar to the Contentsrect and Contentscenter attributes mentioned in chapter II, Anchorpoint is described in unit coordinates , which is the relative coordinates of the layer, the upper-left corner of the layer is {0, 0}, and the lower-right corner is {1, 1}, so the default coordinates are { 0.5, 0.5}. Anchorpoint can be placed outside the extent of the layer by specifying that the X and Y values are less than 0 or greater than 1.
Note in Figure 3.3, when the changed Anchorpoint,position property remains fixed, the value does not change, but the frame moves.
And on what occasion does it need to change anchorpoint? Since we can change the position of the layer at will, wouldn't it be confusing to change the anchorpoint? To illustrate this, let's take a practical example and create a project that simulates an alarm clock.
The clock face and clock are made up of four images (Figure 3.4), for the sake of simplicity, we still load and load images in a traditional way, using four Uiimageview instances (you can, of course, set the contents image of their layers with a normal view).
Figure 3.4 Four pictures of clock faces and clocks
The components of the alarm clock are arranged by IB (Figure 3.5), which are nested within a container view, and the auto-adjust and auto-layout are disabled. This is because auto-tuning affects the frame of the view, and as shown in Figure 3.2, when the view is rotated, the frame is changed, which can cause some layout failures.
We use the Nstimer to update the alarm clock, use the view's Transform property to rotate the clock (if you are not familiar with this property, do not worry, we will be in the 5th Chapter "transformation" in detail), the specific code see listing 3.1
Figure 3.5 Layout The alarm view in Interface Builder
List 3.1 Clock
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@interface viewcontroller () @property (nonatomic, weak) iboutlet uiimageview *hourhand; @property (nonatomic, weak) iboutlet uiimageview *minutehand; @property (nonatomic, weak) iboutlet uiimageview *secondhand; @property (nonatomic, weak) NSTimer *timer; @end @implementation viewcontroller -(void) Viewdidload { [super viewDidLoad]; //start Timer self.timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (tick) userinfo:nil repeats:yes]; ? //set Initial Hand Positions [self tick]; } -(void) Tick { //convert time to hours, minutes and seconds nscal endar *calendar = [[Nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsuinteger units = nshourcalendarunit | nsminutecalendarunit | NSSecondCalendarUnit; nsdatecomponents *components = [Calendar components:units fromdate:[nsdate date] ]; cgfloat Hoursangle = (components.hour/12.0) * M_PI * 2.0; //calculate hour hand angle//calculate minute hand angle cgfloat Minsangle = (components.minute/60.0) * M_PI * 2.0; //calculate second hand angle cgfloat Secsangle = (Components.second/ 60.0) * M_PI * 2.0; //rotate Hands self.hourhand.transform = Cgaffinetransformmakerotation (Hoursangle); self.minutehand.transform = Cgaffinetransformmakerotation (MinsAngle); self.secondhand.trAnsform = Cgaffinetransformmakerotation (Secsangle); } @end |
Running the project looks a little strange (Figure 3.6), because the picture of the clock revolves around the center, which is not a fulcrum we expect.
Figure 3.6 clock face, and misaligned clock hands
You might think that you can adjust the position of the pointer image in Interface builder, but it doesn't work, because if you don't put it in the middle of the clock, it doesn't rotate correctly.
It may be a solution to add a transparent space at the end of the picture, but it will make the picture bigger and consume more memory, which is not elegant.
The better scenario is to use the Anchorpoint property, and we'll add a few lines of code to the-viewdidload method to translate the anchorpoint of each clock pointer (listing 3.2), and figure 3.7 shows the correct result.
Listing 3.2
| 1 2 3 4 5 6 7 8 9 ten |
-(void) Viewdidload { [super viewDidLoad]; //adjust anchor Points self.secondhand.layer.anchorpoint = Cgpointmake (0.5f, 0.9f); self.minutehand.layer.anchorpoint = Cgpointmake (0.5f, 0.9f); self.hourhand.layer.anchorpoint = Cgpointmake (0.5f, 0.9f); //start timer} |
Figure 3.7 clock face, and correctly aligned clock hands
[IOS animation]-calayer layer geometry