Uiview programming guide Study Notes

Source: Internet
Author: User
Tags xform
Viewcreating and processing ing view objects creating view objects using interface Builder

Using interface builder is a well-known method of using storyboard to create pages. It is better to use storyboard for UI implementation or programatic. Further research is required.

Creating view objects programmatically

The default initialization function of the view is initwithframe:, which sets the coordinates and size of the device view relative to the superview, as follows:

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

In this way, a view with coordinates and sizes is created.

Setting the properties of a view

Uiview provides many attributes for users to use. You can modify these attributes to achieve the desired effect.

These attributes may not always be used, but you need to know that at least one such attribute exists, so that they can be used in the future.

Tagging views for future identification

Uiview has a tag attribute. With this attribute, we can use viewwithtag of uiview to obtain the corresponding view. Note that this function is traversed through depth first.

 

Creating and managing a view hierarchy

This section mainly introduces the creation and management of the view hierarchy, that is, the management of the view object.

Adding and removing subviews
  1. You can use the following functions to add, delete, and reorder subviews.
  • Addsubview:
  • Insertsubview:
  • Reorder:
    • Bringsubviewtofront:
    • Sendsubviewtoback:
    • Exchangesubviewatindex: withsubviewatindex
  • Removefromsuperview:

2. when the subview is out of the bonds range of the superview, the extra part is displayed first by default. If you want to remove the extra part, you can set clipstobonds = yes for the view.

3. addsubview is widely used in the following two places

  • In application: didfinishlaunchingwitexceptions:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    // Add the view controller‘s view to the window and display.    [window addSubview:viewController.view];    [window makeKeyAndVisible];return YES; }

 

  • Manage view hierarchy in loadview or viewdidload Function
    • Loadview --------- building your view programmatically
    • Viewdidload ----- creat view programmatically or load them from nib
Hiding views

1. You can use the following method to hide a view

  • Hidden = Yes
  • Alpha = 0.0.

2. After the view is hidden, it will not be displayed. But it will participate in the auto-layout process of the interface. Therefore, when an interface appears again in an hour, hidden is more efficient than removing a view.

3. responds to events of the hidden view

When a view is hidden, it cannot respond to the events!

But! If the view to be hidden is the current first responder, the state of Fisrt responder will not be deregistered when it is hidden, so you need to force change this state.

4. About the hidden Animation

Many people need to add an animation to the view during hidden. It should be noted that if the hidden attribute is used, the animation will not be realistic. Only the Alpha attribute can be used for hidden to add the desired animation.

Locating views in a view hierarchy

There are two ways to get the desired view in view hierarchy

  • Save the pointer reference of the view and use it next time.
  • Get the view using viewwithtag through the tag attribute of the View
Translating, scaling, and rotating views

Here we mainly talk about the changes in the view, such as how to implement the zoom-in and rotation operations.

1. The transform attribute of uiview contains a cgaffinetransform structure. By default, this attribute will not affect the view,

However, we can assign values to the transform attribute at any time to change the view status.

For example, to make the view rotate 45 degrees:

// M_PI/4.0 is one quarter of a half circle, or 45 degrees.CGAffineTransform xform = CGAffineTransformMakeRotation(M_PI/4.0);self.view.transform = xform;

 

2. Transform operations are performed based on the center ponit of the view.

3. scaling a view only changes the length and width of an image without changing its center point.

Converting coordinates in the view hierarchy

Here, we will mainly coordinate the coordinates of views in viewhierarchy under different coordinate reference systems.

1. For uiview, we can use the following two sets of functions for conversion.

  • Convertpoint: fromview:/convertpoint: toview:
  • Convertrect: fromview:/convertrect: toview:

 

The conversion process and results will be well interpreted:

 

The blue and pink dotted boxes represent two different views (a view in blue and B view in pink). We call the rectangle to be converted M rectangle.

At this point, we can see that the position of the M rectangle in a view is a blue solid rectangle, and the M rectangle is converted into a pink solid rectangle in B view. It is equivalent to a projection. They have their own reference systems.

 

Updating ....

 

The original Article is reprinted. indicate the source. Thank you!

Uiview programming guide Study Notes

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.