iOS开发之-View 2-Window and Views

Windows

1.  window object has several responsibilities:

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.

2. In iOS, windows do not have any visual adornments. A window is always just a blank container for one or more views. 

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.

2. To create a window programmatically
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


Adding Content to Your Window

1. Each window typically has a single root view object (managed by a corresponding view controller) that contains all of the other views representing your content.
[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.

3. When configuring the root view of the window, you are responsible for setting its initial size and position within the window. If the root view of your window is provided by a container view controller (such as a tab bar controller, navigation controller, or split-view controller), you do not need to set the initial size of the view yourself. 

Views

Creating and Configuring View Objects

1. The top level of the nib file usually contains a single view object that represents your view controller’s view. The view controller itself is typically represented by the File’s Owner object.

Creating View Objects Programmatically

CGRect  viewRect = CGRectMake(0, 0, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];

1. After you create a view, you must add it to a window (or to another view in a window) before it can become visible. 

Properties of UIView

1. alpha, hidden, opaque
These properties affect the opacity of the view. The alpha and hidden properties change the view’s opacity directly.
The opaque property tells the system how it should composite your view. 

2. bounds, frame, center, transform
The transform property is used to animate or move the entire view in complex ways. For example, you would use a transform to rotate or scale the view. If the current transform is not the identity transform, the frame property is undefined and should be ignored.

3. autoresizingMask, autoresizesSubviews
These properties affect the automatic resizing behavior of the view and its subviews. The autoresizingMask property controls how a view responds to changes in its parent view’s bounds. The autoresizesSubviews property controls whether the current view’s subviews are resized at all.

4. contentMode, contentStretch, contentScaleFactor
These properties affect the rendering behavior of content inside the view. The contentMode and contentStretch properties determine how the content is treated when the view’s width or height changes. The contentScaleFactor property is used only when you need to customize the drawing behavior of your view for high-resolution screens.

5. gestureRecognizers, userInteractionEnabled, multipleTouchEnabled, exclusiveTouch

6. backgroundColor, subviews, drawRect: method, layer, (layerClass method)

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

 

Adding and Removing Subviews

1. To add a subview to a parent, call the addSubview: method of the parent view. This method adds the subview to the end of the parent’s list of subviews.

2. To insert a subview in the middle of the parent’s list of subviews, call any of the insertSubview:... methods of the parent view. 

3. To reorder existing subviews inside their parent, call the bringSubviewToFront:, sendSubviewToBack:, or exchangeSubviewAtIndex:withSubviewAtIndex: methods of the parent view.

4. To remove a subview from its parent, call the removeFromSuperview method of the subview (not the parent view).

5.  A subview whose frame lies outside of its superview’s visible bounds is not clipped by default. If you want your subview to be clipped to the superview’s bounds, you must explicitly set the clipsToBounds property of the superview to YES.

6. Another common place where you might add subviews to a view hierarchy is in the loadView or viewDidLoad methods of a view controller. If you are building your views programmatically, you put your view creation code in the loadView method of your view controller. Whether you create your views programmatically or load them from a nib file, you could include additional view configuration code in the viewDidLoad method.

7. Superviews automatically retain their subviews, so after embedding a subview it is safe to release that subview.
if you remove a subview from its superview and intend to reuse it, you must retain the subview again.

8. When you add a subview to another view, UIKit notifies both the parent and child views of the change. willMoveToSuperview:, 
willMoveToWindow:, 
willRemoveSubview:, 
didAddSubview:, 
didMoveToSuperview, 
or didMoveToWindow methods. 

9. After creating a view hierarchy, you can navigate it programmatically using the superview and subviews properties of your views. The window property of each view contains the window in which that view is currently displayed (if any). Because the root view in a view hierarchy has no parent, its superview property is set to nil. For views that are currently onscreen, the window object is the root view of the view hierarchy.

Hiding Views

1. To hide a view visually, you can either set its hidden property to YES or change its alpha property to 0.0. A hidden view does not receive touch events from the system.

2. If you want to animate a view’s transition from visible to hidden (or the reverse), you must do so using the view’s alpha property. The hidden property is not an animatable property, so any changes you make to it take effect immediately.

Locating View in a View Hierarchy

1. There are two ways to locate a views in a hierarchy:
  (1)Store pointers to any relevant views in an appropriate location
  (2)Assign a unique integer to each view’s tag property and use the viewWithTag: method to locate it.

Translating, Scaling, and Rotating Views

1. Every view has an associated affine transform that you can use to translate, scale, or rotate the view’s content.

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. 


Converting Coordinates in View Hierarchy

1.  touch events report the location of each touch in the window’s coordinate system but view objects often need that information in the view’s local coordinate system.

UIView
convertPoint:fromView:
convertRect:fromView:
convertPoint:toView:
convertRect:toView:

UIWindow
convertPoint:fromWindow:
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.






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:

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


1. View objects are responder objects—instances of the UIResponder class—and are therefore capable of receiving touch events. When a touch event occurs, the window dispatches the corresponding event object to the view in which the touch occurred. If your view is not interested in an event, it can ignore it or pass it up the responder chain to be handled by a different object.


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. 





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值