In-depth analysis of iPhone windows and views

Source: Internet
Author: User
IPhone windows and views are the content to be introduced in this article, mainly to understand the use of windows and views in iPhone. Not to mention, let's take a look at the details. 1. Program Create a uiwindowuiwindow * awindow = [[[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds] autorelease]; 2. In the iPhone OS system, because a view must have an associated layer object, the uiview class will automatically create the corresponding layer during initialization. You can access this layer through the layer attribute of the view, but you cannot change the layer object after the view is created. If you want a view to use different types of layers, you must reload its layerclass class method and return the layer object you want to use in this method. The most common reason for using different layer classes is to implement an OpenGL-based application. In order to use the OpenGL plotting command, the layer below the view must be an instance of the caeagllayer class. This type of layer can interact with the OpenGL rendering call to display the expected content on the screen. 3. Code You can use the frame, bounds, and center attributes for different purposes. The boundary rectangle represents the local coordinate system of the view. Therefore, in the plotting and event processing code, it is often used to obtain the location where an event or update is required in the view. The center point represents the center of the view. Changing the center point is always the best way to move the view position. The border rectangle is a convenient value calculated through the bounds and center attributes. It is valid only when the view transform attribute is set to a constant transformation. 4. If a view must have some subviews to work, you should create them in its initwithframe: method to ensure that the subview can be displayed and initialized together with the view. If the child view is part of the application design (not required for view work), it should be created outside the view initialization code. In iPhone applications, two views are most commonly used to create views and subviews. They are the applicationdidfinishlaunching: method and loadview method of the View Controller of the application delegate object. 5. A touch event usually uses the coordinate value of the window indicator system to report the event location. However, the view object needs the location information relative to the local coordinate of the view, and the two may be different. 6. When the layout of a view changes at any time, uikit activates the automatic size adjustment of each view and calls the layoutsubviews method, this gives you the opportunity to further adjust the geometric size of the Child view. The situations listed below will change the view layout: the size of the view boundary rectangle changes. The content offset of the scroll view, that is, the origin of the visible content area, is changed. The conversion matrix associated with the view changes. The core animation sub-layer group associated with the view layer changes. Your application calls the setneedslayout or layoutifneeded method of the view to force the layout. Your application calls the setneedslayout method of the layer object behind the view to force the layout. 7. When you change the view content, you can use setneedsdisplay or setneedsdisplayinrect: to notify the System of the part to be repainted. After the application returns a running loop, all tracing requests are merged, and the parts to be updated on the computing interface are traversed, send a drawrect: Message to the view to be updated. The starting point of traversal is the root view of the view hierarchy, and then its subviews are traversed from the back to the front. The drawrect method must be implemented for a view that displays custom content within a visible boundary to render the content. 8. Each new view object you define should contain initwithframe: initialization method. This method initializes the class when creating an object to make it in a known state. This method is required when you use code to create your view instance. If you load a custom View class instance from the NIB file, you need to know that in iPhone OS, the code for loading nib does not use the initwithframe: method to create a new view object, instead, the nscoding Protocol defines the initwithcoder: method. Even if your view adopts the nscoding protocol, interface builder does not know its custom attributes, so it does not know how to encode those attributes into the NIB file. Therefore, when you load a custom view from a NIB file, the initwithcoder: method does not have the information required for correct initialization. To solve this problem, you can implement the awakefromnib method in your own class, especially for custom classes loaded from nib files. 9. If you are sure that your drawing code always overwrites the entire view surface with opaque content, you can set the opaque attribute Declaration of the view to yes, to improve the overall efficiency of the tracing code. When you identify a view as opaque, uikit will avoid plotting the content at the bottom of the view. This not only reduces the overhead of plotting, but also reduces the work required for content merging. However, this attribute can be set to yes only when you can determine that the content provided by the view is not transparent. If you cannot ensure that the view content is always opaque, you should set it to No. 10. Another Way to Improve the painting performance (especially in the rolling process) is to set the clearscontextbeforedrawing attribute of the view to No. When this attribute is set to yes, uikit will fill the area to be updated by this method with transparent black before calling the drawrect: method. Setting this attribute to no can cancel the filling operation, and the application is responsible for completely re-painting to the part of the updated rectangle of the drawrect: method. Such optimization is usually a good compromise in the rolling process. 11. when processing a touch event, uikit uses the hittest: withevent: And pointinside: withevent: Methods of the uiview to determine whether the touch event occurs in the specified view. Although these methods are rarely required to be reloaded, You can overload them to make the subview unable to handle touch events.

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.