Windows
It contains your application’s visible content.
It plays a key role in the delivery of touch events to your views and other application objects.
It works with your application’s view controllers to facilitate orientation changes.
Creating and Configuring a Window
1. You create the window at launch time and should retain it and store a reference to it in your application delegate object.self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
Adding Content to Your Window
[self.window addSubview:self.viewController.view];
2. In place of the preceding code, you can alternatively configure the rootViewController property of the window in your nib file. If this property is set when the window is loaded from its nib file, UIKit automatically installs the view from the associated view controller as the root view of the window.
Views
Creating and Configuring View Objects
Creating View Objects Programmatically
CGRect viewRect = CGRectMake(0, 0, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
The opaque property tells the system how it should composite your view.
Tagging Views for Future Identification
The UIView class contains a tag property that you can use to tag individual view objects with an integer value. You can use tags to uniquely identify views inside your view hierarchy and to perform searches for those views at runtime.The default value for the tag property is 0.To search for a tagged view, use the viewWithTag: method of UIView. It does not search superviews or other parts of the view hierarchy. Thus, calling this method from the root view of a hierarchy searches all views in the hierarchy but calling it from a specific subview searches only a subset of views.
Creating and Managing a View Hierarchy
![](http://hi.csdn.net/attachment/201111/23/0_1322012788ADKs.gif)
CGAffineTransform xform = CGAffineTransformMakeRotation(M_PI/4.0);
self.view.transform = xform;
2. The order in which you add those transformations to the CGAffineTransform structure is significant.
convertRect:fromView:
convertPoint:toView:
convertRect:toView:
convertRect:fromWindow:
convertPoint:toWindow:
convertRect:toWindow:
Adjusting the Size and Position of Views at Runtime
Whenever the size of a view changes, the size and position of its subviews must change accordingly.Handling Layout Changes Automatically Using Autoresizing Rules
1. The autoresizesSubviews property of the superview determines whether the subviews resize at all. If this property is set to YES , the view uses the autoresizingMask property of each subview to determine how to size and position that subview. Size changes to any subviews trigger similar layout adjustments for their embedded subviews.![](http://hi.csdn.net/attachment/201111/25/0_1322187101up17.gif)
![](http://hi.csdn.net/attachment/201111/25/0_1322187142gTs9.gif)
Tweaking the Layout of Your Views Manually
1. Whenever the size of a view changes, UIKit applies the autoresizing behaviors of that view’s subviews and then calls thelayoutSubviews method of the view to let it make manual changes. Your implementation of this method can do any of the following:
- Adjust the size and position of any immediate subviews.
- Add or remove subviews or Core Animation layers.
- Force a subview to be redrawn by calling its setNeedsDisplay or setNeedsDisplayInRect: method.
Defining a Custom View
Checklist for Implementing a Custom View- Define the appropriate initialization methods for your view:
- For views you plan to create programmatically, override theinitWithFrame: method or define a custom initialization method.
- For views you plan to load from nib files, override theinitWithCoder: method. Use this method to initialize your view and put it into a known state.
- To handle any custom drawing, override thedrawRect: method and do your drawing there.
- Set the autoresizingMask property of the view to define its autoresizing behavior.
- If your view class manages one or more integral subviews, do the following:
- Create those subviews during your view’s initialization sequence.
- Set the autoresizingMask property of each subview at creation time.
- If your subviews require custom layout, override thelayoutSubviews method and implement your layout code there.
- To handle touch-based events, do the following:
- Attach any suitable gesture recognizers to the view by using theaddGestureRecognizer: method.
- For situations where you want to process the touches yourself, override thetouchesBegan:withEvent:,touchesMoved:withEvent:, touchesEnded:withEvent:, and touchesCancelled:withEvent: methods. (Remember that you should always override thetouchesCancelled:withEvent: method, regardless of which other touch-related methods you override.
Implementing Your Drawing Code
1. Before calling your view’s drawRect: method, UIKit configures the basic drawing environment for your view. Specifically, it creates a graphics context and adjusts the coordinate system and clipping region to match the coordinate system and visible bounds of your view. Thus, by the time your drawRect: method is called, you can begin drawing your content using native drawing technologies such as UIKit and Core Graphics. You can get a pointer to the current graphics context using theUIGraphicsGetCurrentContext function.
2. The current graphics context is valid only for the duration of one call to your view’sdrawRect: method. UIKit might create a different graphics context for each subsequent call to this method, so you should not try to cache the object and use it later.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect myFrame = self.bounds;
// Set the line width to 10 and inset the rectangle by
// 5 pixels on all sides to compensate for the wider line.
CGContextSetLineWidth(context, 10);
CGRectInset(myFrame, 5, 5);
[[UIColor redColor] set];
UIRectFrame(myFrame);
}
3. If you know that your view’s drawing code always covers the entire surface of the view with opaque content, you can improve system performance by setting the opaque property of your view to YES.
Responding to Events
2. In addition to handling touch events directly, views can also use gesture recognizers to detect taps, swipes, pinches, and other types of common touch-related gestures.
3. If you prefer to handle touch events directly, you can implement the following methods for your view, which are described in more detail in Event Handling Guide for iOS:
touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
touchesCancelled:withEvent:
4. The default behavior for views is to respond to only one touch at a time. If the user puts a second finger down, the system ignores the touch event and does not report it to your view. If you plan to track multifinger gestures from your view’s event-handler methods, you need to enable multitouch events by setting the multipleTouchEnabled property of your view to YES
5. Some views, such as labels and images, disable event handling altogether initially. You can control whether a view is able to receive touch events by changing the value of the view’s userInteractionEnabled property.
6. As it handles touch events, UIKit uses the hitTest:withEvent: and pointInside:withEvent: methods of UIView to determine whether a touch event occurred inside a given view’s bounds.