Ios's understanding of the anchor
There are not many places for anchor in ios, most of which are used in animation.
Today, I saw an animation. It was all about the anchor. The concept of anchor came into contact with cocos2d two years ago. I didn't think much about it. Today I saw it, just learn it.
I read a blog about the anchor. I will refer to some of the above images:
The OpenGL ES coordinate system is used in cocos2d, and the coordinate origin is in the lower left corner of the screen. Ios uses the Quartz 2D coordinate system, with the coordinate origin in the upper left corner of the screen.
In cocos2d and ios, set the coordinate points of the view to (10, 10). The result is as follows:
So what is an anchor? The following is an example:
For example, to create the following two views, the upper left corner of the Blue View is at the coordinate (5, 4), while the right side of the orange view is aligned with the Blue View, and half of the height is outside the Blue View.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CrC01dVpb3Ox6te8tcS0tL2oytPNvLXE0LS3qL/J0tTV4tH50LS0 + sLro7o8L3A + CjxwcmUgY2xhc3M9 "brush: java;">
- UIView * blueView = [[UIView alloc] initWithFrame: CGRectMake (5, 4, W, H)];
- BlueView. backgroundColor = [UIColor blueColor];
- [Self. view addSubview: blueView];
-
- UIView * orangeView = [[UIView alloc] initWithFrame: CGRectMake (W-w, H-h/2, w, h)];
- OrangeView. backgroundColor = [UIColor orangeColor];
- [BlueView addSubview: orangeView];
You can see that the coordinates in the upper left corner of the view are calculated when creating the view, which is very troublesome. The code using the anchor can be written as follows:
- UIView *blueView = [[UIView alloc] initWithSize:CGSizeMake(W, H)];
- [blueView setPosition:CGPointMake(5, 4) atAnchorPoint:CGPointMake(0, 0)];
- blueView.backgroundColor = [UIColor blueColor];
- [self.view addSubview:blueView];
-
- UIView *orangeView = [[UIView alloc] initWithSize:CGSizeMake(w, h)];
- [orangeView setPosition:CGPointMake(W, H) atAnchorPoint:CGPointMake(1, 0.5)];
- orangeView.backgroundColor = [UIColor orangeColor];
- [blueView addSubview:orangeView];
It can be seen that the use of the anchor saves the effort of the computation process. Of course, the anchor is also designed for the Child view. After the anchor is mastered, we do not need to compute the x and y coordinates.Take the above example for analysis:
Use the left point (W, H) of the Blue view in the top view as its own anchor (1, 0.5). [Note: the anchor is located on itself, this point maps the coordinates of a parent view one by one. You can use these two values to calculate the view of the child view. frame. origin ].
The coordinate range of the anchor is as follows:
This is the anchor in the Quartz 2D coordinate system.
Next, let's take a look at the method of using the vertices of the parent view as their own anchor points in the code.
- (void)setPosition:(CGPoint)point atAnchorPoint:(CGPoint)anchorPoint{ CGFloat x = point.x - anchorPoint.x * self.width; CGFloat y = point.y - anchorPoint.y * self.height; [self setOrigin:CGPointMake(x, y)];}