Detailed explanation of very useful methods and properties of iOS UIView

http://www.cnblogs.com/snake-hand/p/3190021.html

Detailed explanation of very useful methods and properties of iOS UIView
type="font-family:Simsun; font-size:16px; line-height:24px">


in Before calling the view's drawRect: method, UIKit will automatically configure the drawing environment so that the upper left corner becomes the origin of the
coordinate system, and all Quartz calls that occur in this environment can draw correctly in the view.

View objects track their size and position through the frame, bounds, and center property declarations. The frame property contains a rectangle, the border rectangle, that specifies the position and size of the view relative to its superview's coordinate system. The bounds property also contains a rectangle, the bounding rectangle, that defines the position and size of the view relative to the local coordinate system. Although the origin of the bounding rectangle is usually set to (0, 0), this is not required. The center property contains the center point of the border rectangle.

When you create a view object in code with the initWithFrame: method, its frame property is set. This method also initializes the origin of the bounds rectangle to (0.0, 0.0), which is the same size as the view's bounding box. Then the center property will be set to the center point of the border.

By default, the borders of views are not clipped by the borders of the parent view. If you want a view to clip
its subviews, set its clipsToBounds property to YES.

For example, the UIView class contains a transform property declaration that you can use to perform various types of pan, zoom, and zoom effects on the entire view. By default, the value of this property is an identity transform that does not change the appearance of the view. Before adding the transformation, first get the CGAffineTransform structure stored in this property, use the corresponding Core Graphics function to perform the transformation, and then reassign the modified transformation structure to the transform property of the view.

A view's contentMode property determines the effect of border changes and zoom operations on the view.
By default, the value of this property is set to UIViewContentModeScaleToFill, which means that the view content is always scaled to fit the new border size.

Different UIViewContentMode constants (such as UIViewContentModeTop and
UIViewContentModeBottomRight) can cause the current content to be displayed in different corners of the view or along different borders of the view, and there is a mode that can display the content in the center of the view.

Always consider using content mode when you want to implement resizable controls in your application.
Content mode is often useful to avoid drawing the contents of a view, but you can also use UIViewContentModeRedraw mode when you want specific control over the view's appearance during scaling and resizing.

If the view's autoresizesSubviews property declaration is set to YES, its
Subviews are automatically resized according to the value of the autoresizingMask property. Otherwise, the application must provide its own implementation by overriding the layoutSubviews method.

If you want to keep the relative position of the lower left corner of a view and its parent view unchanged, you can add UIViewAutoresizingFlexibleRightMargin and
UIViewAutoresizingFlexibleTopMargin constants, and assign the result to the autoresizingMask property. When multiple sections on the same axis are set to be variable, the sizing allowance is evenly distributed among the sections.

UIViewAutoresizingNone If this constant is set, the view will not be automatically resized.
If the constant UIViewAutoresizingFlexibleHeight
is set, the height of the view will be proportional to the height of the
superview . Otherwise, the height of the view will remain the same.
UIViewAutoresizingFlexibleWidth
If this constant is set, the width of the view will be proportional to the width of the
superview . Otherwise, the width of the view will remain the same.
UIViewAutoresizingFlexibleLeftMargin
If this constant is set, the left margin of the view will
be . Otherwise, the position
of remains unchanged.
If the constant UIViewAutoresizingFlexibleRightMargin
is set, the right border of the view will
be . Otherwise, the relative position
of remains unchanged.
UIViewAutoresizingFlexibleBottomMargin
If this constant is set, the bottom border of the view will
be . Otherwise, the view's position relative
to remains unchanged.
If the constant UIViewAutoresizingFlexibleTopMargin
is set, the top border of the view will
be . Otherwise, the relative position
of remains the same.




If you configure views through Interface Builder, you can use the Size viewer's Autosizing controls to set the automatic sizing behavior for each view. The flexible width and height constants in the image above have the same behavior as the co-located springs in Interface Builder, but the blank constants have the opposite behavior. In other words, if you want to apply the automatic sizing behavior of the flexible right margin to a view in Interface Builder, you must empty the Autosizing control for the corresponding directional space instead of placing a strut.

If a view's autoresizesSubviews property is set to NO, all autoresizing behavior of the view's immediate subviews will be ignored. Similarly, if a subview's autoresizing mask is set to
UIViewAutoresizingNone, the subview will not be resized, and thus its immediate subviews will not be resized.

The parent-child relationship in the view hierarchy helps us define the chain of objects in the application responsible for handling touch events

When you create a new view object, you need to allocate memory for it and send the object an initWithFrame: message to initialize it. For example, if you wanted to create a new instance of the UIView class as a container for other views, you could use the following code:
CGRect viewRect = CGRectMake(0, 0, 100, 100);
UIView* myView = [[UIView alloc ] initWithFrame: viewRect];

In iPhone programs, there are two places that are most commonly used to create views and subviews. They are the applicationDidFinishLaunching: method of the application delegate object and the loadView method of the view controller.

To add a view, call the parent view's addSubview: method, which adds a view to the end of the list of subviews.
Call the insertSubview:... method of the parent view to insert a view in the middle of the parent view's list of subviews.
Call the parent view's bringSubviewToFront: , sendSubviewToBack: , or
exchangeSubviewAtIndex:withSubviewAtIndex: method to reorder the parent view's subviews. Using these methods is slightly faster than removing the subview from the parent view and inserting it again.
The subview can be removed from the superview by calling the removeFromSuperview method of the subview (rather than the superview).

Create a window with a view
- (void) applicationDidFinishLaunching:(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 square
CGRect redFrame = CGRectMake(10, 10, 100, 100);
UIView *redView = [[UIView alloc] initWithFrame:redFrame];
redView.backgroundColor = [UIColor redColor];
// Create a simple blue square
CGRect 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 child view to a view, UIKit sends several messages to the corresponding parent and child views, notifying them of the current state change. You can override methods like willMoveToSuperview: ,
willMoveToWindow: , willRemoveSubview: , didAddSubview: , didMoveToSuperview , and didMoveToWindow in your custom view to do the necessary processing before and after the event, and update based on changes View state information.

After the view hierarchy is established, you can get its superview through the view's superview property, or get the view's subviews through the subviews property. 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 has no superview, so its superview 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 hierarchy.

The UIView class defines the following methods for coordinate conversion between different view local coordinate systems:
convertPoint:fromView:
convertRect:fromView:
convertPoint:toView:
convertRect:toView:
The UIWindow version uses the window coordinate system.
convertPoint:fromWindow:
convertRect:fromWindow:
convertPoint:toWindow:
convertRect:toWindow:

The UIView class contains a tag property. With this property, you can identify a view object by an integer value. You can use this property to uniquely identify the view in the view hierarchy, and to retrieve the view at runtime (retrieval based on tag identification is faster than traversing the view hierarchy yourself). The default value of the tag attribute is 0. You can retrieve the tagged views through UIView's viewWithTag: method. An

animation block begins with a call to the UIView's beginAnimations:context: class method and ends with a call to the commitAnimations class method. Between these two calls, you can configure the parameters of the animation and change the values ​​of the properties you wish to animate. Once the commitAnimations method is called, UIKit will begin performing animations that animate the transition of a given property from its current value to its new value. Animation blocks can be nested, but nested animations will not be executed until the outermost animation block is committed.

frame The bounding rectangle of the view, in the superview's coordinate system.
bounds The bounding rectangle of the view, in the view's coordinate system.
center The center of the border, in the superview's coordinate system.
transform The transformation matrix on the view, relative to the center of the view's bounds.
alpha The alpha value of the view, used to determine the transparency of the view.

Use the setAnimationStartDate: method to set the date the animation will occur after the commitAnimations method returns. The default behavior is to cause the animation to execute immediately on the animation thread.
Use the setAnimationDelay: method to set the interval between when the animation actually occurs and the point in time returned by the commitAnimations method.
Use the setAnimationDuration: method to set the number of seconds the animation will last.
Use the setAnimationCurve: method to set the relative speed of the animation process. For example, the animation may gradually speed up during the enlightenment phase and gradually slow down during the end phase, or maintain the same speed throughout the process.
Use the setAnimationRepeatCount: method to set the number of repetitions of the animation.
Use the setAnimationRepeatAutoreverses: method to specify whether the animation automatically reverses when it reaches the target value. You can use this method in conjunction with the setAnimationRepeatCount: method to smoothly transition each property between its initial and target values ​​for a period of time.

You can set the delegate through the UIView's setAnimationDelegate: class method, and specify the selector method that receives the message
through the setAnimationWillStartSelector: and setAnimationDidStopSelector: methods.
The form of 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 are the same as those passed to the beginAnimations:context: method at the beginning of the animation block:
animationID - a string provided by the application to identify the animation in an animation block.
context - is also an application-provided object used to pass additional information to the delegate object.
setAnimationDidStopSelector: The selector method has one more parameter - a boolean value. The value is YES if the animation completed successfully and was not canceled or stopped by other animations.

  Whenever a view's layout changes, UIKit activates each view's automatic resizing behavior and then calls the respective layoutSubviews method, giving you the opportunity to further adjust the subview's geometry.
Your application calls the view's setNeedsLayout or layoutIfNeeded method to force a 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 custom CALayer objects linked
as
In some cases, changes to the application data model will affect the corresponding user interface. To reflect these changes, you can mark the corresponding view as needing to refresh (by calling the setNeedsDisplay or setNeedsDisplayInRect: methods).

You can hide or show a view by changing the view's hidden property declaration.

Initialize a subclass of a view
- (id) initWithFrame: (CGRect)aRect {
self = [super initWithFrame:aRect];
if (self) {
// setup the initial properties of
the view
...
}
return self;
}

In iPhone OS, the
code does not pass initWithFrame : method to instantiate a new view object, but through the initWithCoder: method defined by the NSCoding protocol.

A simple implementation of the drawRect: method draws a 10-pixel wide red
border around the view border . Since UIKit's drawing operations are also implemented based on Quartz, you can mix and match different drawing calls as follows to get the desired result.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect myFrame = self.bounds;
CGContextSetLineWidth(context, 10);
[[UIColor redColor] set];
UIRectFrame(myFrame); // fiddling around, making it complicated! ! !
}

Views that handle touch events usually need to implement all of the following methods
touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
touchesCancelled:withEvent:
activate multi-touch events: The multipleTouchEnabled property declaration is set to YES.
You can control whether a view can handle events by changing the value of the view's userInteractionEnabled property. You can also use the beginIgnoringInteractionEvents and endIgnoringInteractionEvents methods
of the UIApplication object UIKit will use the UIView's hitTest:withEvent: and pointInside:withEvent: methods to determine whether a touch event occurs on the specified view.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360922&siteId=291194637