Recently, when I was using uiview for a project, I was a little bit unique, so I was afraid of errors. So I read the overview of the uiview class in the document again and got the answer to many fuzzy questions, mr. Hou Jie said, "do not build a high platform in the sand ".
The first half of this article is the translation of the uiview overview in this document. The second half is your own understanding and some code examples.
Uiview Overview
Uiview Definition
Uiview class: defines a rectangular area on the screen and some interfaces used to manage content in this area according to the overview of uiview. At runtime, a view object processes the rendering of content in its region (Note: calayer) and the interaction behavior in this region. The uiview class itself provides the basic method of filling the rectangular area with the background color. to display more complex content (Note: for example, the background of the ring set), uview must be subclass-type, and implements plotting and event processing methods. The uikit framework contains a set of standard uview subclasses, from simple buttons (Note: uibutton) to complex tables (Note: uitableview.
Because view objects are the main way for applications to interact with users, they have many responsibilities. For example:
Drawing and Animation:
Drawing Technology, such as uikit, core graphics, and OpenGL ES, draws the content of the rectangular area.
Animation effects can be used when some attributes of a view are changed to new values.
Layout and sub-View Management:
A view can contain zero or multiple views.
Each view defines its default size through its parent view.
When necessary, you can manually change the size and position of the subview.
Event processing:
A view is a responder that can process touch events and other events defined by the uiresponder class.
The view can use addgesturerecognizer: method-installed gesture identification tool to process Common gestures.
A view can be embedded into other views and create complex visual layers. This creates a parent-child relationship embedded view (Child view) and an embedded view (parent view ). Generally, the visible area of a sub-view is not restricted by the boundaries of the parent view, but this setting can be changed in IOS through the clipstobounds attribute. A parent view can contain any number of child views, but a self-view has only one parent view. This parent view is responsible for locating its child views properly.
A view is defined using the geometric attributes of frame, bounds, and center. The frame defines the starting point and size of the view in the coordinate system of the parent view, and is usually used to adjust the size or position of the view in the layout. The center attribute can be used to adjust the view position without changing the view size. Bounds defines the internal size of a view in its own coordinate system and is generally used only for custom rendering code. The size part of the frame and bounds are coupled to change the size of the rectangle. The size part of both is updated.
Create a view
You can use the following code to create a view programmatically:
CGRect viewRect = CGRectMake(10、10、100、100); UIView * myView =[[UIView alloc]initWithFrame:viewRect];
This Code creates a view, which is located in the coordinate system of the parent view (once it is added to the parent view) Point (10, 10). Use addsubview: Method to add the child view to another view. In iOS, sibling views can overlap with each other without any problems and can be placed in complex views. Addsubview: Specifies the view where the method is placed on top of other sibling views. You can add a view by using insertsubview: abovesubview: And insertsubview: belowsubview: to specify the order of zcoordinates of the Self-view. You can also use the exchangesubviewatindex: withsubviewatindex: Method to switch the positions of added views.
When creating a view, it is important to assign an appropriate value to the autoresizingmask attribute to ensure that the view is correctly adjusted. View adjustment mainly occurs in the direction of your application interface, and may also occur at other times. For example, call the setneedslayout method to force your view to update its layout.
View Drawing cycle
A view is drawn based on the needs. When the view is displayed for the first time, or because of layout changes, all or part of the view becomes visible, or the system requires the view to repaint its content. For a view that uses uikit or core graphics to customize the content, the system calls the drawrect: Method of the view. The implementation of your method is responsible for drawing the View content to the current image context set by the method automatically called by the system previously. This will create a visual display of your view content, the content is displayed on the screen.
When you change the actual content of your view, you need to notify the system that your view needs to be repainted. You can call the setneedsdisplay or setneedsdisplayinrect method of your view to achieve this. These methods let the system know that it should update the view in the next rendering cycle. Because the system will wait until the next rendering cycle updates the view, you can call these methods in multiple views to update the view at the same time.
Animation
Changing some view attributes can trigger an animation. The effect is that the changed attributes are used to create an animation that is displayed to users for a short period of time. The actual animation work is mainly done by the View class, but you still need to point out which attributes you want to become an animation. There are two different ways to start an animation:
Use block-based animation after IOS 4 and later. (Recommended)
Use the begin/commit animation method.
The following attributes of a view can be animated.
Thread Problems
The user interface of the application must be operated in the main thread. Therefore, you should call the method of the uiview class when the code runs in the main thread of the application. When creating the view object itself, not all executions must be in the main thread. This is the only situation with no strict constraints.
Subclass description
(Omitted)
Translation part ended
Uiview Definition
In my understanding, the essence of calayer in uiview is a buffer that contains a bitmap and is mainly responsible for rendering. uiview inherits from uiresponder and processes interaction through the response chain. In the cocoatouch framework, all programming is centered around uiview, that is, view-based programming. This relationship is like dialog box programming in MFC, A program consists of several dialogs and Their Relationships. An app consists of several views.
Implementation of drawrect: Method
Implementation of drawrect: This method enables the view to display complex content. Click here for the code. The display of the ring (target) is realized.
- (void)drawRect:(CGRect)rect{ // What rectangle am I filling? CGRect bounds = [self bounds]; // Where is its center? CGPoint center; center.x = bounds.origin.x + bounds.size.width / 2.0; center.y = bounds.origin.y + bounds.size.height / 2.0; // From the center, how far out to a corner? float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0; // Get the context being drawn upon CGContextRef context = UIGraphicsGetCurrentContext(); // All lines will be drawn 10 points wide CGContextSetLineWidth(context, 10); // Set the stroke color to light gray [[UIColor lightGrayColor] setStroke]; // Draw concentric circles from the outside in for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { CGContextAddArc(context, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES); CGContextStrokePath(context); } // Create a string NSString *text = @"You are getting sleepy."; // Get a font to draw it in UIFont *font = [UIFont boldSystemFontOfSize:28]; // Where am I going to draw it? CGRect textRect; textRect.size = [text sizeWithFont:font]; textRect.origin.x = center.x - textRect.size.width / 2.0; textRect.origin.y = center.y - textRect.size.height / 2.0; // Set the fill color of the current context to black [[UIColor blackColor] setFill]; // Set the shadow to be offset 4 points right, 3 points down, // dark gray and with a blur radius of 2 points CGSize offset = CGSizeMake(4, 3); CGColorRef color = [[UIColor darkGrayColor] CGColor]; CGContextSetShadowWithColor(context, offset, 2.0, color); // Draw the string [text drawInRect:textRect withFont:font];}
Sub-View Management
Add a view using the addsubview method. If the sub-view is no longer used, use the removefromsuperview method to disconnect the parent view or window.
Add a gesture instance
After ios5.0, the type of gesture recognition can be directly used. Here we provide a new example of gesture recognition on the official website. Click here to download the code.
Animation call instance
Packageinfo must be the uiview class or its subclass. Replace it with the position of your uiview Instance name when using packageinfo. You can view the animation during view switching.
Begin/conmit Method
[UIView beginAnimations:@"View Easy In" context:nil]; [UIView setAnimationDelay:0.2]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; packageInfo.frame = CGRectMake(0, 180, 200, 260); [UIView commitAnimations];
Block animation method (recommended)
[UIView animateWithDuration:1 delay:0.2 options:UIViewAnimationCurveEaseIn animations:^{packageInfo.frame = CGRectMake(0, 180, 200, 260);} completion:NULL];
I will probably write it here. I want to add something else.