Several basic concepts of the iphone drawing Cgpoint, Cgsize, CGRect, CGRectMake, window, Views (view)

Source: Internet
Author: User

I generally do not use Interface Builder to draw the interface, but with pure code to create the interface, not to install B, but just from VI to Xcode soon, not too accustomed to interface builder. Of course I'll use it if I need to. The existence of a thing is not absolutely good and bad, but the existence of time and space determines its value.

(forgot to say, my environment is xcode4.2)

First you need to understand a few basic concepts.

A) Three structural bodies: Cgpoint, Cgsize, CGRect

1. Cgpoint

[Plain] View plaincopy
    1. /* Points. */
    2. struct Cgpoint {
    3. CGFloat x;
    4. CGFloat y;
    5. };
    6. typedef struct CGPOINT Cgpoint;

See this must you already understand, no longer explain.

2. Cgsize

[Plain] View plaincopy
    1. /* Sizes. */
    2. struct Cgsize {
    3. CGFloat width;
    4. CGFloat height;
    5. };
    6. typedef struct CGSIZE cgsize;

Not explained.

3.CGRect

[Plain] View plaincopy
    1. /* rectangles. */
    2. struct CGRect {
    3. Cgpoint origin;//Offset is relative to the parent window
    4. Cgsize size;
    5. };
    6. typedef struct CGRECT CGRect;

Also not explained.

These three structures are in a header file: CGGeometry.h

II) Several methods

1.CGRectMake

[Plain] View plaincopy
    1. Cg_inline CGRect
    2. CGRectMake (CGFloat x, cgfloat y, cgfloat width, cgfloat height)
    3. {
    4. CGRect rect;
    5. rect.origin.x = x; RECT.ORIGIN.Y = y;
    6. Rect.size.width = width; Rect.size.height = height;
    7. return rect;
    8. }

Yes, this method is make a rect, set the origin (beginning, upper left corner), width and height, you can draw a position and size of a rect (rectangle) This function is declared as an inline function, one because it is smaller, and the second is because we require a certain degree of efficiency in the drawing interface. This function is still hidden in the first file: CGGeometry.h

c) Several basic interface elements: Window (Windows), Views (view)

To display content on the screen first to create a window hosting content, to create a window, you need a border (frame), the underlying structure containing the border information is cgrect. Each object that can display itself on the screen has a border that defines his display area, but many high-level view classes automatically calculate this information. Other classes are set by a initWithFrame initialization method when the view is initialized.

Come to know a class: UIScreen. The UIScreen class represents the screen, and through this class we can get some of the stuff we want.

[Plain] View plaincopy
    1. CGRect screenbounds = [[UIScreen mainscreen]bounds];//returns a rect with the status bar
    2. CGRect viewbounds = [[UIScreen mainscreen]applicationframe];//does not contain a rect of the status bar
    3. Screenbounds and Viewbounds are relative to the device screen.
    4. So screenbounds.origin.x== 0.0;   SCREENBOUNDS.ORINGIN.Y = 0.0;  ScreenBounds.size.width = = 320; ScreenBounds.size.height = = 480 (or some other resolution is different)
    5. So screenbounds.origin.x== 0.0;  SCREENBOUNDS.ORINGIN.Y = 20.0; (because the height of the status bar is 20 pixels) screenBounds.size.width = = 320; ScreenBounds.size.height = = 480

UIView

Here's a look at the UIView class, which inherits from Uiresponder, and we know that it's the canvas that is responsible for the display, if you're comparing window to a frame. We are constantly removing, replacing, or overlaying the canvas on the frame, or overlaying other canvases on the canvas, which is of course the painter's decision. With the canvas, we can do it arbitrarily on top. A lot of simple things I will put the contents of the library, if things too much paste out is not very good, friends go to the library file inside to see it. This class is inside the UIView.h. Below we first learn some basic things, the other stuff will slowly unfold in the future.

[Plain] View plaincopy

    1. uiview* MyView =[[UIView alloc]initwithframe:cgrectmake (0.0,0.0,200.0,400.0)];//Here you create a canvas that defines the position relative to the parent window, and the size.

We can add this canvas to the other canvas, which we'll talk about later in the process. We can also draw on this canvas other interesting things, the specific situation will be explained in the following.

UIWindow

UIWindow inherited from UIView, there may be a logical obstacle to this point, how does the frame inherit from the canvas? Do not go too far to niu Jiao Jian, the shape of the frame is the same as the canvas? Take a canvas and then use some methods to strengthen it, is it possible to use it as a frame? That's why a view can be added directly to another view.

Take a look at the initialization process of the system (inside Application Didfinishlauchingwithoptions):

[Plain] View plaincopy
    1. Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
    2. Self.window.backgroundColor = [Uicolor graycolor];//sets a background color for window

Several basic concepts of the iphone drawing Cgpoint, Cgsize, CGRect, CGRectMake, window, Views (view)

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.