Detailed explanation of IOS uiview's usage methods and attributes

Source: Internet
Author: User

Before calling the drawrect: Method of the view, uikit automatically configures the drawing environment to make the upper left corner

The origin of the coordinate system. The quartz call in this environment can be correctly traced in the view. View objects are stored by frame, bounds, and center
To track your own size and position. The frame attribute contains a rectangle, that is, a border rectangle, used to specify the position and size of a view relative to its parent view coordinate system. The bounds attribute also contains a rectangle, that is, a boundary rectangle, which defines the position and size of the view relative to the local coordinate system. Although the origin of the boundary rectangle is usually set to (0, 0), this is not necessary. The Center Property contains the center point of the border rectangle. When you create a view object using the initwithframe: Method in the code, its frame attribute is set. This method also initializes the origin of the bounds rectangle to (0.0, 0.0), and the size is the same as the border of the view. Then, the center attribute is set to the center of the border. By default, the border of the view is not cropped by the border of the parent view. If you want a view to crop its subviews, set its clipstobounds attribute to yes. For example, the uiview class contains a transform attribute declaration, which allows you to implement various types of translation, proportional scaling, and zoom effects for the entire view. By default, the value of this attribute is a constant transformation and does not change the view appearance. Before adding transformations, you must first obtain the cgaffinetransform Structure Stored in this attribute, and use the corresponding core
Graphics functions implement transformations, and then assign the modified transform structure to the transform attribute of the view. The contentmode attribute of a view determines the effect of border changes and scaling operations on The View. By default, the value of this attribute is set to uiviewcontentmodescaleto. FillThis means that the view content is always scaled to adapt to the new border size. Different uiviewcontentmode constants (such as uiviewcontentmodetop and uiviewcontentmodebottomr) Ight) The current content can be displayed in different corners of the view or along different boundaries of the view. Another mode is to display the content in the center of the view. When you want to adjust the size of a widget in an application, you must consider using the content mode. The content mode usually helps avoid plotting the View content. However, you can also use the uiviewcontentmoderedraw mode when you want to control the view appearance during scaling and resizing. If the autoresizessubviews attribute declaration of a view is set to yes, its subviews are automatically adjusted based on the autoresizingmask attribute value. Otherwise, the application must provide its own implementation by reloading the layoutsubviews method. To keep the relative position of a view and its parent view unchanged, you can add uiviewautoresizingflexib. LerightmarginAnd uiviewautoresizingflexib LetopmarginConstant, and assign the result to the autoresizingmask attribute. When multiple parts in the same axial direction are set to variable, the margin of size adjustment is evenly distributed to each part. If the constant uiviewautoresizingnone is set, the view will not adjust the size automatically. Uiviewautoresizingflexib LeheightIf this constant is set, the height of the view will change proportionally to the height of the parent view. Otherwise, the view height remains unchanged. Uiviewautoresizingflexib LewidthIf this constant is set, the width of the view will change proportionally with the width of the parent view. Otherwise, the view width remains unchanged. Uiviewautoresizingflexib LeleftmarginIf this constant is set, the left boundary of the view is adjusted proportionally as the width of the parent view changes. Otherwise, the relative position of the left boundary of the view and its parent view remains unchanged. Uiviewautoresizingflexib LerightmarginIf this constant is set, the right border of the view is adjusted proportionally as the width of the parent view changes. Otherwise, the relative position of the right boundary of the view and its parent view remains unchanged. Uiviewautoresizingflexib LebottommarginIf this constant is set, the bottom boundary of the view is adjusted proportionally as the height of the parent view changes. Otherwise, the relative position of the bottom boundary of the view and its parent view remains unchanged. Uiviewautoresizingflexib LetopmarginIf this constant is set, the upper boundary of the view is adjusted proportionally as the height of the parent view changes. Otherwise, the relative position of the upper boundary of the view and its parent view remains unchanged.  

  If you configure the view through interface builder, you can use the autosizing control of the size viewer.
To set the automatic size adjustment behavior for each view. The flexible width and height constants in have the same behavior as the springs in the same position in interface builder, but the blank constants act exactly the opposite. In other words, if you want to apply the automatic size adjustment of the flexible right blank to a view in the interface builder, you must leave the autosizing control of the corresponding direction space blank, rather than placing a pillar. If the autoresizessubviews attribute of a view is set to no, all automatic dimension adjustment behaviors of the direct subviews of the view are ignored. Similarly,
If the automatic size adjustment mask of a subview is set to uiviewautoresizingnone, the size of the subview will not be adjusted, and the size of its direct subview will not be adjusted. The parent-child relationship in the view level helps us define the object chain in the application for processing touch events When creating a new view object, you need to allocate memory for it and send an initwithframe: Message to it for initialization. For example, if you want to create a new uiview class instance as a container for other views, you can use the following code: cgrect viewrect = cgrectmake (0, 0,100,100 ); uiview * myview = [[uiview alloc] Initwithframe: viewrect];  In iPhone applications, two views are most commonly used to create views and subviews, which are the applicationdidfinishlaun of the application delegate object. Ching: Method and the loadview method of the View Controller. Call the addsubview method of the parent view to add a View to the end of the subview list. You can call the insertsubview:... method of the parent view to insert a view in the middle of the Child View list of the parent view. Call bringsubviewtofront of the parent view:, sendsubviewtoback:
, Or exchangesubviewatindex: withsubviewatindex: The method can be used to re-sort the child views of the parent view. Using these methods is faster than removing the child view from the parent view and inserting the child view again. You can call the removefromsuperview method of the child view (instead of the parent view) to remove the child view from the parent view. Create a window with a view-(void) applicationdidfinishlaun Ching:( Uiapplication *) Application {// create the window object and assign it to the // window instance variable of the application delegate. window = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] Bounds];Window. backgroundcolor = [uicolor whitecolor]; // create a simple red squarecgrect redframe = cgrectmake (10, 10,100,100); uiview * redview = [[uiview alloc] initwithframe: redframe]; redview. backgroundcolor = [uicolor redcolor]; // create a simple blue squarecgrect blueframe = cgrectmake (10,150,100,100); uiview * blueview = [[uiview alloc] initwithframe: blueframe]; blueview. backgroundcolor = [uicolor bluecolor]; // Add the square views to the window [Window addsubview: redview]; [Window addsubview: blueview]; // once added to the window, release the views to avoid the // extra retain count on each of them. [redview release]; [blueview release]; // show the window. [Window makekeyandvisible];} When you add a subview to a view, uikit sends several messages to the parent and child views to notify them of the current status changes. In your custom view, you can view willmovetosuperview:, willmovetowindow:, willremovesubview:
Didaddsubview:, didmovetosuperview, and didmovetowindow are reloaded to perform necessary processing before and after the event occurs, and update the view status information based on the change. After the view hierarchy is created, you can obtain the parent view through the superview attribute of the view, or obtain the subview of the view through the subviews attribute. You can also use the isdescendantofview: method to determine whether a view is in the view layer of its parent view. The Root View of a view hierarchy does not have a parent view, so its superview
The property is set to nil. For the view currently displayed on the screen, the window object is usually the Root View of the entire view level.   The uiview class defines the following methods for coordinate transformation between local coordinate systems of different views: convertpoint: fromview: convertrect: fromview: convertpoint: toview: convertrect: toview: the version of uiwindow uses the window coordinate system. Convertpoint: fromwindow: convertrect: fromwindow: convertpoint: towindow: convertrect: towindow: The uiview class contains a tag attribute. With this attribute, you can identify a view object by an integer. You can use this attribute to uniquely identify the view in the view hierarchy and view retrieval at runtime (tag-based retrieval is faster than self-traversing the view hierarchy ). The default value of the tag attribute is 0. You can use the viewwithtag method of the uiview to retrieve the identified view. The animation block starts from calling the beginanimations: Context: Class Method of uiview, and calls the commitanimations class.
Method. Between these two calls, you can configure the Animation Parameters and change the attribute values you want to implement the animation. Once the commitanimations method is called, uikit starts to execute the animation, that is, the process of changing the given attribute from the current value to the new value is shown in the animation. The animation block can be nested, but the nested animation will not be executed before the animation block on the outermost layer is submitted. The border rectangle of the frame view, which is located in the coordinate system of the parent view. Bounds view boundary rectangle, located in the coordinate system of the view. Center of the center border, located in the coordinate system of the parent view. The transformation matrix in the transform view, relative to the center of the view boundary. The Alpha value of the Alpha view, used to determine the transparency of the view. Use setanimationstartdate: to set the occurrence date of the animation after the commitanimations method returns. The default action is to immediately execute the animation in the animation thread. Use setanimationdelay: to set the interval between the time points actually returned by the animation and commitanimations. Use setanimationduration: to set the animation duration in seconds. Setanimationcurve is used to set the relative speed of the animation process. For example, an animation may gradually accelerate in the revelation phase, but gradually slow down in the end phase, or keep the same speed throughout the process. Use setanimationrepeatcount: to set the number of animation repetitions. Use setanimationrepeatautore Verses: Specifies whether an animation is automatically played back when it reaches the target value. You can use this method in combination with setanimationrepeatcount: to smoothly switch each attribute between the initial value and the target value for a period of time. You can use the setanimationdelegate: Class method of the uiview to set the delegate, and use setanimationwillstartsel Ector:
And setanimationdidstopselec Tor: Specifies the selector Method for receiving messages. The message processing method is as follows:-(void) animationwillstart :( nsstring *) animationid context :( void *) context;-(void) animationdidstop :( nsstring *) animationid finished :( nsnumber *) finished context :( void *) context; the animationid and context parameters of the preceding two methods are the same as those of the method: animationid-string provided by the application when the animation block starts, used to identify an animation in an animation block. Context-is also an object provided by the application, used to transmit additional information to the delegate object. Setanimationdidstopselec Tor: The selector method also has a parameter-A boolean value. If the animation is successfully completed without being canceled or stopped by another animation, the value is yes.   At any time, when the layout of a view changes, uikit activates the automatic size adjustment of each view, and then calls the layoutsubviews method to further adjust the geometric size of the subview.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. You can also use the layoutsubviews method to adjust the custom calayer object linked to the view layer as a child layer. Sometimes, changes to the application data model may affect the corresponding user interface. To reflect these changes, you can identify the view as needing to be refreshed (by calling setneedsdisplay or setneedsdisplayinrect: method) You can hide or display a view by changing its hidden attribute declaration. Initialize a subclass of a view-(ID) initwithframe :( cgrect) arect {self = [Super initwithframe: arect]; If (Self) {// setup the initial properties ofthe view ...} return self ;} In iPhone OS, the code for loading nib does not use the initwithframe: method to create a new view object. Instead, it uses the initwithcoder: method defined by the nscoding protocol. Drawrect: A simple implementation of the method, that is, draw a red border of 10 pixels wide on the view border. Since the implementation of the uikit painting operation is also based on quartz, you can use different tracing calls as follows to get the expected results. -(Void) drawrect :( cgrect) rect {cgcontextref context = Uigraphicsgetcurrentcont EXT();Cgrect myframe = self. bounds; cgcontextsetlinewidth (context, 10); [[uicolor redcolor] set]; uirectframe (myframe); // it is complicated to use it !!!} To process a touch event view, you usually need to implement all of the following methods: touchesbegan: withevent: touchesmoved: withevent: enabled: withevent: touchescancelled: withevent: activate multi-touch event: multipletouchenabled Property. You can change the userinteractionenabled attribute value of the view to determine whether the view can process events. You can also use the beginignoringinteraction of the uiapplication object. EventsAnd endignoringinteractionev EntsMethod Uikit uses the hittest: withevent: And pointinside: withevent: Methods of uiview to determine whether a touch event occurs in a specified view.
Related Article

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.