Superficial analysis of UIView

Source: Internet
Author: User

the uiview  class defines a rectangular area, the screen, and the interfaces for managing the content in this area. At runtime, a-view object handles the rendering of any content in it area and also handles any interactions with that con Tent. The uiview  class itself provides basic behavior for filling Its rectangular area with a background color. More sophisticated content can is presented by Subclassing uiview & Nbsp;and implementing the necessary drawing and Event-handling code yourself. The UIKit framework also includes a set of standard subclasses. range from simple buttons to complex tables and can is Used As-is. For example, A uilabel object draws a text string and A uiimageview  object draws an image.

Because View objects is the main your application interacts with the user, they has a number of responsibilities. Here is just a few:

  • Drawing and animation

    • Views draw content in their rectangular area using technologies such as UIKit, Core Graphics, and OpenGL ES.

    • Some View properties can animated to new values.

  • Layout and Subview Management

    • A view may contain zero or more subviews.

    • Each view defines their own default resizing behavior in relation to their parent view.

    • A view can define the size and position of its subviews as needed.

  • Event Handling

    • A view is a responder and can handle touch events and other events defined by the UIResponder class.

    • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

Views can embed and create sophisticated visual hierarchies. This creates a parent-child relationship between the view being embedded (known as the Subview) and the parent VI EW doing the embedding (known as the Superview). Normally, a subview's visible area isn't clipped to the bounds of it superview, but on IOS you can use the clipsToBounds propert Y to alter that behavior. A Parent view may contain any number of subviews but each subview have only one superview, which are responsible for Positio Ning its subviews appropriately.

The geometry of a view is defined by itsframe,bounds, andcenterProperties. TheframeDefines the origin and dimensions of the view in the coordinate system of it superview and is commonly used during layout To adjust the size or position of the view. ThecenterProperty can is used to adjust the position of the view without changing its size. Thebounds defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing Co De. The size portion of the frame and bounds rectangles is coupled together so that changing the size of either rectangle Updates the size of both.

For detailed information UIView the "about" use of the class, see View ProgrammingGuide for IOS.

NOTE

In IOS 2.x, the maximum size of an UIView object is a 1024x768 x points. In IOS 3.0 and later, views is no longer restricted to this maximum size but is still limited by the amount of memory th EY consume. It's in your best interests to keep view sizes as small as possible. Regardless of which version of IOS is running, you should consider tiling any content that's significantly larger than th E Dimensions the screen.

Creating a View

To create a view programmatically, you can use code like the following:

    1. cgrect viewRect  = cgrectmake (10, < Span class= "Mi" >10, 100, 100
    2. uiview* myview  = [[uiview alloc ] initwithframe:viewrect]; 

this code creates the view and positions it at the Point (Ten) in its Superview ' s coordinate system (on Ce it is added to that Superview). To add a subview to another view, you use The addsubview:  method. In IOS, sibling-overlap each of the other without any issues, allowing complex view placement. The addsubview:  method places the specified view on top of other siblings. You can specify the relative z-order of a subview by adding it using The insertsubview:above Subview:  and insertsubview:belowsubview:  methods. You can also exchange the position of already added subviews using The exchangesubviewatind Ex:withsubviewatindex: method. 

When creating a view, it was important to assign an appropriate value to the property to autoresizingMask ensure the view resizes Corr ectly. View resizing primarily occurs when the orientation of your application ' s interface changes but it could happen at other Tim Es as well. For example, calling the setNeedsLayout method forces your view to update its layout.

The View Drawing Cycle

view drawing occurs on an as-needed basis. When a view is first shown, or if all or part of it becomes visible due to layout changes, the system asks the view to D Raw its contents. For contain custom content using UIKit or Core Graphics, the system calls the view ' S 

When the actual content of your view changes, it's your responsibility to notify the system that your view needs to be re Drawn. Calling your view ' s or method of the setNeedsDisplay setNeedsDisplayInRect: view. These methods let the system know the It should update the view during the next drawing cycle. Because it waits until the next drawing cycle to update the view, you can call these methods on multiple views to update t Hem at the same time.

NOTE

If you have the using OpenGL ES to does your drawing, you should use the GLKView class instead of subclassing UIView . For more information on how to draw using OpenGL ES, see OpenGL ES programmingGuide for IOS.

For detailed information on the view drawing cycle and the role your views has the this cycle, see View programming Guide for IOS.

Animations

Changes to several view properties can be animated-that are, changing the property creates a animation that conveys the CH Ange to the user through a short period of time. The class does most of the work of UIView performing the actual animations and you must still indicate which property Chang Es you want to be animated. There is different ways to initiate animations:

    • In IOS 4 and later, use the Block-based animation methods. (Recommended)

    • Use the Begin/commit animation methods.

The Block-based animation methods (such as animateWithDuration:animations: ) greatly simplify the creation of animations. With one method call, you specify the animations to be performed and the options for the animation. However, block-based animations is available only in IOS 4 and later. If your application runs on earlier versions of IOS, you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.

The following properties of the UIView class is animatable:

    • @property frame

    • @property bounds

    • @property center

    • @property transform

    • @property alpha

    • @property backgroundColor

    • @property contentStretch

For more information on how to configure animations, see View ProgrammingGuide for IOS.

Initiate

1) inherit from Uiresponder, so there is a touch and gesture method, directly rewrite can be implemented in the current view of the specific operation of the processing.

    • A view is a responder and can handle touch events and other events defined by the UIResponder class.

    • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

2) with animation ability. The Block-based animation methods (such as animateWithDuration:animations: ); Use blocks to quickly implement key-frame animations and base animations.

3) have the method to draw your own DrawRect: The system will first call this method to draw on the current drawing text, this creates a static visual representation of your view's content that can then Be displayed on the screen, is static visible. So when you change the contents of the view after the system is drawn, you need to manually call when the actual content of your view changes, it's your responsibility to notify the system tha T your view needs to be redrawn. You does this by calling your view's setNeedsDisplay or setNeedsDisplayInRect: method of the view to force the system to update the view content in the next drawing cycle so that the view updates as you want.

4) for drawing use the UIKit or Core Graphics, know the difference between the two and the connection, and how to use it, so that you can customize the view's drawing content.

5) Understanding UIView's several properties, center,frame,bounds, it is important to understand them relative to that view.

Superficial analysis of UIView

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.