ios development of UIView and UIViewController

UIView represents a rectangular area on the screen, responsible for rendering the content area, and in response to a touch event occurs in this region. It occupies an important position in the absolute iOS App, as a subclass of iOS in almost all visual controls are the UIView.

UIView may be responsible for the following tasks:

  • Rendering and animation
  • Layout and subview management
  • Event Processing

Rendering and animation

View Draw

UIView drawn on demand, when a part due to changes in layout, becomes visible, you will be asked to view the entire view or view draw. For those who need to use UIKit view or CoreGraphics custom drawing, the system calls the  drawRect: method to paint.

When a view content changes, you need to call  setNeedsDisplay or  setNeedsDisplayInRect: method, which tells the system to redraw the view of. After calling this method, the system will draw the next cycle update this view. Because the system should wait until the next draw cycle really draw, you can call one time for multiple views  setNeedsDisplay, they will also be updated.

Geometric properties view

There view frame, center, bounds, and several other basic geometric properties, wherein:

  • Most frame used, the position coordinates are relative to the parent view, this view may be used to determine the position in the parent view and the size of its own
  • center coordinate position is relative to the parent view, generally for moving, and rotating animation operational
  • bounds with respect to itself, usually is (0,0, width, height), meaning that the range bounds may be allowed to view the current drawn

ContentMode view

View after the initial drawing is complete, the system will draw results are a snapshot after snapshot to use as much as possible, avoid redrawn. If the geometric properties of the view changes, the system will be to decide how to change the display according to contentMode view.

The default contentMode Shi  UIViewContentModeScaleToFill , the system will stretch current snapshot to make it conform to the new frame size. Most of the current snapshot contentMode will stretch or move operation. If needs to be redrawn, contentMode can be set  UIViewContentModeRedraw, call force view when changing the size of the operation and the like drawRect:redraw.

Animation

Here you can change the view of these properties in the form of animation, just tell the system to start the animation and the value at the end, the system will automatically handle the transition process in the middle.

frame
bounds
center
transform
alpha
backgroundColor
contentStretch

 

Layout and subview management

In addition to providing content itself outside view, a view may behave like a container. When a view contains another view, between the two views to create a parent-child relationship. This relationship is called subView in neutron view, parent view is called superView. A view can comprise a plurality of sub-views that are stored in this view the  subviews array. Add, delete, and the operation of these sub-views of the relative positions of the function is as follows:

addSubview:
insertSubview:...
bringSubviewToFront:
sendSubviewToBack:
exchangeSubviewAtIndex:withSubviewAtIndex:
removeFromSuperview(子视图调用)

 

AutoResizing 和 Constraint

When changing the size of a view, its position and size of sub-views need to be changed accordingly. UIView support automatic layout, you can manually child view been laid.

When following these events occur, the need for layout operations:

  • bounds size of the view change #
  • Rotating user interface, usually leads to a change in size of the root view controller
  • Core Animation sublayers layer layer changed view
  • Program call view setNeedsLayoutor layoutIfNeededmethod
  • The program calls the view layer setNeedsLayoutmethod

Auto Resizing

View autoresizesSubviewsproperty determines the size when the view is changed, automatically regulates subview.

Mask can be used are as follows:

UIViewAutoresizingNone
UIViewAutoresizingFlexibleHeight
UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleTopMargin

 

Bit operators can combine them, for example  UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth.

Constraint

Constraint is another method for automatic layout. Essentially, Constraint is a constraint between UIView two attributes:

attribute1 == multiplier × attribute2 + constant

 

Wherein both sides of the equation is not necessarily equal to the relationship, the relationship may be greater than or equal the like.

Constraint more flexible and powerful than AutoResizing, you can implement complex sub-view layout.

Custom layout

UIView which provides a  layoutSubviews function, UIView subclass can override this function, to achieve a more complex and sophisticated layout child View.

Apple document specifically emphasizes, it should be used only when the effect Autoresizing and Constraint mechanisms mentioned above can not achieve the required before use  layoutSubviews. Moreover, layoutSubviews method can only be triggered call system, programmers can not directly call the manual method.

So the timing layoutSubviews specific method calls, what does? There are several specific situations:

  1. In the case of autoresize mask of the parent view is ON, addSubview will lead to add the view calls layoutSubviews, at the same time add the target view and all of its sub-view will be called.
  2. setFrame When a new frame and old is not the same (ie view of the size of changed) calls layoutSubviews
  3. Scroll a UIScollView cause this scrollView and its parent View call layoutSubviews
  4. View the main rotating equipment can cause the current response ViewController call layoutSubviews
  5. View the size change will cause the parent to call layoutSubviews View
  6. removeFromSuperview parent View will lead to calls layoutSubviews

Event Processing

UIView subclass UIResponder, and you can respond to touch events.

May be used generally  addGestureRecognizer: added in response to a touch gesture recognition events, if need to manually handle, press UIView need to reload the following four functions:

touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
touchesCancelled:withEvent:

 

 

The UIViewController (View Controller), as the name implies, the controller is part of the MVC design pattern. In UIKit UIViewController the main function is to control switching screens, wherein the view attribute (type the UIView) manage the appearance of the entire screen.

UIViewController life cycle

The first step is to initialize the ViewController life cycle. However, the specific method calls which had been different. If you use StoryBoard to create ViewController, we do not need to explicitly initialized, Storyboard will automatically  initWithCoder: initialized. If you do not use StoryBoard, we can use the  init: function to initialize init: function will be invoked in the implementation process  initWithNibName:bundle:. We should try to avoid calling in outside the VC  initWithNibName:bundle:, but put it inside the VC (refer here ).

After initialization is complete, VC's life cycle will go through the following several functions:

(void)loadView
(void)viewDidLoad
(void)viewWillAppear
(void)viewWillLayoutSubviews
(void)viewDidLayoutSubviews
(void)viewDidAppear
(void)viewWillDisappear
(void)viewDidDisappear

 

Suppose now that there is a AViewController (abbreviated Avc) and BViewController (abbreviated Bvc), to achieve a jump Bvc Avc by push navigationController, the following is the order of the execution of the method:

1. A viewDidLoad  
2. A viewWillAppear  
3. A viewDidAppear  
4. B viewDidLoad  
5. A viewWillDisappear  
6. B viewWillAppear  
7. A viewDidDisappear  
8. B viewDidAppear 

 

If then jump back from Avc Bvc, will have the following order of execution:

1. B viewWillDisappear  
2. A viewWillAppear  
3. B viewDidDisappear  
4. A viewDidAppear  

 

ViewDidLoad visible only called once, and then jump back Avc second time, AViewController still in memory, it does not need to load up.

Note that the above life cycle are not mentioned in relation to ViewController destroyed, there is a in iOS 4 & 5 in the ViewController  viewDidUnload method. When out of memory, the application receives Memory warning, the system will automatically call the current method did not viewDidUnload ViewController at the interface. Typically, these are not displayed on the interface ViewController UINavigationController Push stack ViewController is not in the stack, and a sub ViewController UITabBarViewController not shown. The View Controller will be in the Memory Warning event occurs, the system automatically calls viewDidUnload method.

Starting iOS 6, viewDidUnload method was discarded, and applications are also no longer call viewDidUnload method when memory warning. We can reload  - (void)didReceiveMemoryWarning and  -(void)dealloc cleanup work to.

Guess you like

Origin www.cnblogs.com/qisi007/p/11073227.html