Ios's understanding of the anchor

Source: Internet
Author: User

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;">

  1. UIView * blueView = [[UIView alloc] initWithFrame: CGRectMake (5, 4, W, H)];
  2. BlueView. backgroundColor = [UIColor blueColor];
  3. [Self. view addSubview: blueView];
  4. UIView * orangeView = [[UIView alloc] initWithFrame: CGRectMake (W-w, H-h/2, w, h)];
  5. OrangeView. backgroundColor = [UIColor orangeColor];
  6. [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:

       
       
    1. UIView *blueView = [[UIView alloc] initWithSize:CGSizeMake(W, H)];
    2. [blueView setPosition:CGPointMake(5, 4) atAnchorPoint:CGPointMake(0, 0)];
    3. blueView.backgroundColor = [UIColor blueColor];
    4. [self.view addSubview:blueView];
    5. UIView *orangeView = [[UIView alloc] initWithSize:CGSizeMake(w, h)];
    6. [orangeView setPosition:CGPointMake(W, H) atAnchorPoint:CGPointMake(1, 0.5)];
    7. orangeView.backgroundColor = [UIColor orangeColor];
    8. [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)];}


Related Article

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.