UIView represents a rectangular area on the screen that occupies an absolutely important place in the app because almost all visual controls in iOS are UIView subclasses. Responsible for rendering the content of the area and responding to touch events that occur within that area
UIView features 1. Manage content in a rectangular area 2. Handle events in a rectangular area 3. Management of child views 4. The subclasses of the animated UIView also have these features
is the inner level of the view
1) Three structural bodies cgpoint, cgsize, CGRect
1. cgpoint struct Cgpoint {cgfloat x; CGFloat y; }; typedef struct CGPOINT Cgpoint; See this must you already understand, no longer explain. 2.CGSize struct Cgsize {cgfloat width; CGFloat height; }; typedef struct CGSIZE cgsize; Not explained. 3.CGRect struct CGRect {cgpoint origin; Offset is the cgsize size relative to the parent view; }; typedef struct CGRECT CGRect; Also not explained. These three structures are in a header file: CGGeometry.h
2) The most basic properties of the view
Frame and Center are relative to the parent view, and the bounds is relative to its own
Frame is CGRect frame's origin is relative to the parent view's upper-left origin (0,0) position, changing the view of the frame will change the center
Center is cgpoint refers to the entire view of the central point, changing the view of the centre will also change the frame
Bounds is CGRect is the origin of the view that tells the child view (in layman's terms, the difference between the origin of the child view frame and the origin of the parent view's bounds is the position of the child view relative to the upper-left corner of the parent view, and the child view outside the parent view if the result is negative)
By Addsubview: This method adds a subclass, regardless of who adds it, as soon as it is added later, the view is in the upper layer
Removing the parent view also removes it from the child view.
3) Several basic interface elements: window, view
1.uiview below to meet the UIView class, this class inherits from Uiresponder, look at this name we know it is responsible for the display of the canvas, if the window is compared to a picture 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. uiview* MyView =[[UIView alloc]initwithframe:cgrectmake (0.0,0.0,200.0,400.0)]; //creates a canvas here that defines the position relative to the parent window, as well as 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. 2.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): self.window = [[[UIWindow alloc] initwithframe:[[ UIScreen mainscreen] bounds] [autorelease]; self.window.backgroundcolor = [Uicolor Graycolor]; //set a background color for window [self.window makekeyandvisible]; //Let window show up
The 3.UIScreen class represents the screen, and through this class we can get some of the stuff we want.
CGRect screenbounds = [[UIScreen mainscreen]bounds]; Returns a rect with the status bar cgrect viewbounds = [[UIScreen mainscreen]applicationframe]; Rect//screenbounds and viewbounds that do not contain the status bar are relative to the device screen//So screenbounds.origin.x== 0.0; SCREENBOUNDS.ORINGIN.Y = 0.0; ScreenBounds.size.width = = 320; ScreenBounds.size.height = = 480 (or other resolution varies)//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//Get StatusBar position and size [Self.view Addsubview:thetoolbar]; CGRect statusbarrect = [[UIApplication sharedapplication]statusbarframe]; NSLog (@\ "%@\", Nsstringfromcgrect (Statusbarrect));
4. Practical Walkthrough
Not finished I will use a comprehensive example to summarize my study results, thoroughly understand what I learned, I think since the choice to write code, it is necessary to understand the principle, otherwise only know that it does not know why it is not able to do a good programmer. Create a new project choose empty application name lw1?2) in application didfinishlaunchingwithoptions inside, you will find the system has been built good one picture frame, We now use the system to help us build the picture frame, you can certainly build a frame, but it is not necessary, forget to tell, an application can only have a picture frame. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions { self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds] autorelease];//system to help you build a frame //test it yourself. The difference between bounds and aplicationframe cgrect bound = [[UIScreen mainscreen]bounds]; nslog (@ "boundwidth:%f boundheight:%f", Bound.size.width, Bound.size.height); nslog (@ "boundx:%f boundy:%f", bound.origin.x, BOUND.ORIGIN.Y); cgrect appbound = [[UIScreen mainscreen]applicationframe]; nslog (@ "appboundwidth:%f appboundheight:%f" , Appbound.size.width,appbound.size.height); nslog (@ "appboundx:%f appboundy:%f", appbound.origin.x, APPBOUND.ORIGIN.Y); //painting the first canvas is painted blue, the size is X 100 cgrect CGone = CGRectMake (0.0, 0.0, 320, 100);//Draw a rectangle, initialize position and size uiview *v_one = [[UIView alloc] initwithframe:cgone];//Initialize view v_one.backgroundcolor = [UIColor blueColor];// Painted blue [self.window addsubview:v_one];//directly onto the frame //Second block note its position cgrect cgtwo = CGRectMake (0.0, 100, 160, 100);// Draw a rectangle, initialize position and size uiview *v_two = [[UIView alloc]initwithframe:cgtwo];//Initialize View v_two.backgroundColor = [Uicolor redcolor];//painted red [self.window addsubview:v_two];//overlay to frame //Third note his position cgrect cgthree = CGRectMake (n., +); & nbsp uiview *v_three = [[UIView alloc]initwithframe:cgthree]; v_three.backgroundcolor = [UIColor greencolor]; [self.window addsubview:v_three]; //Fourth block take note of its location cgrect cgfour = CGRectMake (0.0, 260, [+]); uiview *v_four = [[UIView ALLOC]INITW ithframe:cgfour]; v_four.backgroundcolor = [Uicolor orangecolor]; & nbsp [self.window addsubview:v_four];// //Fifth, calculate its position and see how it works, //You can try to move this code to the top of the first quick initialization, and there will be unexpected effects cgrect cgfive = CGRectMake (100, 150, 160, 200); uiview *v_five = [[UIView alloc]iNitwithframe:cgfive]; v_five.backgroundcolor = [Uicolor Yellowcolor]; [self.window addsubview:v_five]; self.window.backgroundcolor = [Uicolor graycolor]; [self.window makekeyandvisible]; //Last remember release [V_one release]; [V_two release]; [V_three release]; [V_four release]; [V_five release]; return YES; } 4) UIView method A UIView can contain a lot of Subview (other UIView), and these Subview with each other is a so-called hierarchical relationship, This is somewhat similar to the concept of a layer in a drawing software, and the following code shows several methods commonly used on the management layer (Subview), with the following code. 1. The first is the new and removed subview that are most commonly used by everyone. ? [Subview Removefromsuperview]; //Remove Subview from the current UIView [uiview Addsubview:subview]; //adds a subview 2 to UIView. Move Subview forward or backward in UIView, moving forward will cover the Subview of the lower layer, And move forward will be the upper SubvieW is covered. ? [UIView Bringsubviewtofront:subview]; //Move subview forward one layer (swap position with its previous layer)//move Subview forward one layer (swap position with its previous layer) [UIView Sendsubviewtoback:subview]; //moves the subview back one layer (with its next layer swapped position) 3. Use the index in UIView to exchange the layer level of the two subview each other. [uiview Exchangesubviewatindex:indexa WITHSUBVIEWATINDEX:INDEXB]; //swap two layers 4. Use the variable name of Subview to get its index value in UIView. Nsinteger index = [[UIView subviews] Indexofobject:subview name]; //get index 5. Add Nsinteger to the Subview, so that they distinguish each other. ? [Subview Settag:nsinteger]; //plus marker [UIView Viewwithtag:nsinteger]; //The view return value is uiview 6 by the tag. Finally, get all the Subview in UIView, call this method to return a nsarray, and list these Subview in a forward order, All the subview in root are listed in the sample image. ? [UIView Subviews]; //UIView all subview articles under the original address: http://blog.csdn.net/xingboss3/article/ details/7890238
View hierarchies, inheritance relationships, and common structure of IOS