"Some summary of iOS7" 8, crack the secret of coordinate system in UIView: about frame, bound and center in UIView

Source: Internet
Author: User
Tags home screen

For beginners who are new to iOS, UIView frame, bound, and center are really prone to thinking, especially if you want to understand the concept of bound in the view class, it really takes some effort. After a series of reference materials and hands-on experiments, finally some of these concepts have some understanding, recorded in this can be for the same confused classmate reference.

First post a blog to do a reference, http://blog.csdn.net/mad1989/article/details/8711697; This blog post has made some elaboration on UIView's frame and bound, But I still feel that some places are vague, so here are some of their own experiments to add to it, in order to let the reader no longer have any doubts.

To create a new empty project in Xcode, add the following program to the Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions function of the APPDELEGATE.M file:

UIView *view1 = [[UIView alloc] Initwithframe:cgrectmake ((a);    [View1 Setbounds:cgrectmake (+,-)];    [Self.window Addsubview:view1];        UIView *view2 = [[UIView alloc] Initwithframe:cgrectmake (0, 0, +)];view2.backgroundcolor = [Uicolor redcolor];
   
    [view1 Addsubview:view2];    NSLog (@ "View1 frame:%@========view1 bounds:%@========view1 center:%@", Nsstringfromcgrect (View1.frame), Nsstringfromcgrect (View1.bounds), Nsstringfromcgpoint (View1.center));    NSLog (@ "View2 frame:%@========view2 bounds:%@========view2 center:%@", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect (View2.bounds), Nsstringfromcgpoint (View2.center));    [View1 release]; [View2 release];
   

After the program executes, we will display our newly added two view objects on the home screen as shown in.


In addition, on the Console Output window, the following is also displayed:
View1 frame:{{50, (+), {200}}========view1 bounds:{{0, 0}, {GB, 200}}========view1 center:{150, 200}
View2 frame:{{0, 0}, {100}}========view2, bounds:{{0, 0}, {50}, 100}}========view2 center:{50
In the above program, we added two rectangle view objects on the main window, the parent view is green, the starting point is (50,100), and the length width is (200,200). When adding the Red sub-view view2, because we are using the initialization method is initWithFrame (there is no Initwithbound method), View2 set its relative position and size in the parent view coordinate system, that is, relative to the parent view coordinates, The starting point is the origin (that is, the upper-left corner) and the size is (100,100). If you change its initial frame, its position relative to the parent view also changes. If the code generated in the program is changed to VIEW2:

UIView *view2 = [[UIView alloc] Initwithframe:cgrectmake (20, 20, 80, 80)];

The program runs the following results:


At the same time, the VIEW2 console output information is changed accordingly:

View2 frame:{{20, {80}}========view2 bounds:{{0, 0}, {A, 80}}========view2 center:{60, 60}.

We also found that, by default, the bound starting point of the view is always (0,0);
And now I think of the point that if we change the bound member of the View object, what will happen to that object?

Add code:

[View2 Setbounds:cgrectmake (50, 50, 100, 100)];
At the same time, undo the changes just to its frame, still (0,0,100,100). The results of the program running are as follows:

Compared to the first picture, there is no change at all. In other words, changing the starting point of the bound does not cause the view itself to move relative to the parent view's position. Because, like this, the bound start point is set to (50,50), which changes the coordinates of the child view itself. When the view is created, the initWithFrame function constructs a child view with reference to the location of the parent coordinates, so changes to the coordinates of the child view itself do not affect itself. Who is the difference between the starting point and the bound?
To explore this problem, we add the following statement after View1 is created:

[View1 Setbounds:cgrectmake (25, 25, 200, 200)];

At this point the View1 's own coordinates are changed. The results of the program run are as follows:


As you can see, the location of the green parent view remains unchanged, but the location of the Red child view moves the distance of the top-left latitude and longitude by 25 pixels. Why is this happening? The starting point of the parent view bound is set to (25,25), which means that the local coordinates of the pixels in the upper-left corner of the green block are no longer (0,0), but (25,25). When the child view's starting render position is at the (0,0) point of the parent view, the corresponding move is made to the upper-left corner of the parent view.
In order to verify this conclusion, we modify the following program again to change the VIEW2 's drawing statement to:

UIView *view2 = [[UIView alloc] Initwithframe:cgrectmake (25, 25, 100, 100)];

Then run the program and show the results as follows:


As you can see, the child view and the upper-right corner of the parent view are coincident when the child view is drawn relative to the parent view, which proves our result from another angle.

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.