"iOS Dev-View"

Source: Internet
Author: User

First, UIWindow object

Each app has a UIWindow object, which, like a container, is used to contain all the views in the app, and the app creates and sets the UIWindow object at startup.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {     // Override point for customization after application launch.    Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];         // Add Viewcontroller    Viewcontroller *controller = [[Viewcontroller alloc] init];     
........................
= Controller; [Self.window Setbackgroundcolor:[uicolor Whitecolor]; [Self.window makekeyandvisible]; return YES;}

The view that joins the window becomes a child view of the window, and the child view can also contain its own sub-view, which is a hierarchical relationship similar to Android view.

Second, UIView

1) Each uiview has a layer property that corresponds to an object of the Calayer class. Then all layers are combined into an image that is drawn on the screen.

2) The Frame property of UIView holds the size of the view and the position relative to the parent view, described with CGRect (it is a struct struct)

3) The difference between frame and bounds: frame is used to determine the relative position of other views in other view hierarchies, while bounds is used to determine the drawing area and avoid drawing outside the layer boundaries.

Third, Core Graphics

The Core graphics is the most important "object" in the Api,core graphics of a set of C languages that provide 2D drawing functionality is the graphics context (graphics context), which is the "object" of Cgcontextref, is responsible for storing the drawing state (such as brush color and line thickness) and the amount of memory in which the drawing content is located.

- (void) DrawRect: (cgrect) rect{//get the context of a drawingCgcontextref CurrentContext =Uigraphicsgetcurrentcontext (); //Save the current contextcgcontextsavegstate (CurrentContext); //Shadows can only be set through the Core graphics at this timeCgcontextsetshadow (CurrentContext, Cgsizemake (4,7),3); //The shape drawn here has a shadow effect//Restore the previous contextcgcontextrestoregstate (CurrentContext); //the image drawn here does not have a shadow effect. }

Where the contextual context of the graph was created before drawrect.

Four, draw a triangle with Uibezierpath

    //define the path of the gradientUibezierpath *mypath =[[Uibezierpath alloc] init]; //use a mypath to a triangle[MyPath Movetopoint:cgpointmake ( the,142)]; [MyPath Addlinetopoint:cgpointmake (260,446)]; [MyPath Addlinetopoint:cgpointmake ( -,446)]; [MyPath Addlinetopoint:cgpointmake ( the,142)]; [MyPath setlinewidth:Ten]; //Set Fill Color[[Uicolor Redcolor]setfill];        [[Uicolor Redcolor] setstroke];    [MyPath fill]; [MyPath stroke];

Note the way to draw triangles: look for a point, and then draw three lines at this point.

V. Run loops and redraw views

The iOS app starts with a running loop (run loop) that loops to listen for events, such as touch events. When the corresponding event is heard in view, the program first executes the processing logic of the corresponding event (such as changing the background color of an element on the screen when the screen is clicked), until the logic is executed, giving control to the running loop.

The run loop first checks to see if there is a view waiting to be redrawn (which requires view to set the Setneedsdisplay manually), and then sends a DRAWRECT message to the view that needs to be redrawn to display the new view on the screen. Redrawing is certainly time-consuming, and iOS is optimized in two ways:

1) Do not redraw the view that the content has not changed

2) Only one DrawRect message occurs in each event processing cycle, meaning that no matter how much the view changes during the event cycle, only one drawrect is executed at the end.

In addition Setneedsdisplayinrect is in order to specify that only one area is redrawn. Normally we don't specify a region.

PS: Encountered a problem, in the UIView sub-class, The listener touch event is not executed.

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event

Then just change the order of the code in the Appdelegate, the problem is solved, and I don't want to understand why this is the case:

      = Controller;    [Self.window Setbackgroundcolor:[uicolor Whitecolor];        [Self.window makekeyandvisible];    [Self.window Addsubview:bnrview];

is to place [Self.window Addsubview:bnrview] behind [Self.window makekeyandvisible].

Liu, Uiscrollview

Uiscrollview has several properties and methods to note:

1) contentsize determines how much area of view Uiscrollview can display (for example, Uiscrollview is like a camera, and Contentsize decides to show up to an area).

2) pagingenable function is to have two pages of data, when sliding to the second page, release the finger can be automatically positioned to the second page. This effect is very common on the mobile side. The principle is that Uiscrollview will split the contentsize into multiple areas of the same size according to its bounds size, and after the drag is over, Uiscrollview automatically scrolls and displays only one of the areas.

The following code is a two-size view in a uiscrollview:

Self.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; //Add ViewcontrollerViewcontroller *controller =[[Viewcontroller alloc] init]; //Create two CGRect structures as frame for Uiscrollview and Bnrhypnosisview objectsCGRect Screenrect =Self.window.bounds; CGRect Bigrect=Screenrect;//bigRect.size.height *=2.0;BigRect.size.width *=2.0; //Create a Uiscrollview object, set its size to the window sizeUiscrollview *scrollview =[[Uiscrollview alloc] initwithframe:screenrect];        [ScrollView Setpagingenabled:yes]; //Create a large size bnrhypnosisview (modify to create a wide view with screen etc.)Bnrhyponsisviewtwo *hyponsisview =[[Bnrhyponsisviewtwo alloc] initwithframe:screenrect];        [ScrollView Addsubview:hyponsisview]; Screenrect.origin.x+=ScreenRect.size.width; Bnrhyponsisviewtwo*antherview =[[Bnrhyponsisviewtwo alloc] initwithframe:screenrect];        [ScrollView Addsubview:antherview]; //Setting the framing range of the ScrollViewScrollview.contentsize =bigrect.size; Self.window.rootViewController=Controller;        [Self.window Setbackgroundcolor:[uicolor Whitecolor]; [Self.window makekeyandvisible];//[Self.window Addsubview:bnrview];[Self.window Addsubview:scrollview];

"iOS Dev-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.