UI basic view ---- UIView summary, ui view ---- uiview

Source: Internet
Author: User
Tags uicontrol

UI basic view ---- UIView summary, ui view ---- uiview

UIView is the most basic view class in the UIKit framework. It is a subclass of UIResponder and a sibling class of UIApplication and UIViewController. It is the parent class of UIWindow, UILabel, UIImageView, UIScrollView, and UIControl, it is UIButton, the parent class of UITextField (they are subclasses of UIControl), and the parent class of UITableView and UICollectionView (they are subclasses of UIScrollView ). In the UIKit framework, it is very important to master the inheritance relationships between views. For example, we know that the parent class of UILabel is UIView, therefore, many attributes and methods of UIView can be directly used after UILabel inherits from the past. Similarly, UIView inherits from UIResponder. It is also necessary to understand the attributes and methods of UIResponder and will be written in the summary of UIResponder.

Because UIView is the parent class of many common view controls, its attributes and methods can be directly inherited to the subclass. Therefore, it is particularly important to understand the common attributes and methods of UIView. Here we will make a simple summary, and pay attention to the similarities and differences of some details, which will be improved in the future.

According to the official document, UIView can be divided into seven common modules based on different purposes (of course, more than these), namely basic configuration, geometric configuration, hierarchical relationship, rendering, animation, gesture, and constraint.

 

0: Preparations

First, add the attributes of two uiviews.

 

1 @property (nonatomic, strong) UIView * view1;2 @property (nonatomic, strong) UIView * view2;

 

 

 

1: basic configuration

Basic configuration includes instantiation, user interaction, and tag value.

Note:

1: User Interaction: the user interaction of UIView is enabled by default, and the user interaction between UILabel and UIimageView in its subclass is disabled by default (because these two focuses on text or images ), therefore, the user interaction function is enabled by default for the sub-classes inherited from UIView, but the above two are disabled. If you need to use interaction, you need to open their user interaction (for example, adding a gesture to an image ).

2: tag value: Generally, the tag value we create must be larger and more regular, because the small tag value may conflict with the system definition and is regular and convenient to use.

 

1/** 2*1: basic configuration 3 */4-(void) basisConfig 5 {6 // 1: two common instantiation Methods 7 self. view1 = [[UIView alloc] init]; 8 self. view2 = [[UIView alloc] initWithFrame: CGRectMake (100,300,200,100)]; 9 // 2: basic attribute 10 self. view1.userInteractionEnabled = YES; // user interaction. The default value is 11 self. view2.userInteractionEnabled = NO; 12 // 3: tag value 13 self. view1.tag = 1001; 14 self. view2.tag= 1002; 15}

 

 

 

2: geometric configuration

There are four types of ry: frame, bounds, center, and transform.

Note:

1: Differences and relationships between frame, bounds, and center: frame is relative to the parent view, including coordinates and dimensions. Bounds is relative to itself. The coordinates are (0, 0), and the center is the center coordinate of the view.

2: The difference between the two ways of writing transform: make is compared with the initial view, that is, no matter what kind of transformation is done later, this transformation is relative to the initial view, therefore, the subsequent transformations may overwrite the previous ones. Another way of writing is relative to the previous transformation, so continuous transformation can be implemented.

 

1/** 2*2: geometric configuration 3 */4-(void) geometryConfig 5 {6 // 1: frame 7 self. view1.frame = CGRectMake (100,100,200,100); 8 // 2: bounds 9 self. view1.bounds = CGRectMake (0, 0,200,100); 10 self. view2.bounds = CGRectMake (0, 0,200,100); 11 // 3: center12 self. view1.center = CGPointMake (self. view. center. x, self. view. center. y); 13 self. view2.center = self. view. center; // the same effect as above 14 // 4: transform15 self. view1.transform = CGAffineTransformMakeRotation (0.1); // Rotate 16 self. view2.transform = CGAffineTransformRotate (self. view2.transform, 0.7); // rotate 17 self. view1.transform = CGAffineTransformMakeTranslation (50, 50); // translate 18 self. view2.transform = CGAffineTransformTranslate (self. view2.transform,-100,100); // Pan 19 self. view1.transform = CGAffineTransformMakeScale (0.5, 0.5); // proportional scaling 20 self. view2.transform = CGAffineTransformScale (self. view2.transform, 0.5, 0.5); // proportional scaling 21}

 

 

 

3: hierarchical relationship

Hierarchical relationships mainly include hierarchical relationships between views and operations on hierarchical relationships between views. The method is flexible. You can switch, add, delete, insert, and put any layer view on the top, bottom, and switch.

 

1/** 2*3: hierarchical relationship 3 */4-(void) hierarchyConfig 5 {6 // 1: add to parent view 7 [self. view addSubview: self. view1]; // when the view comes, the parent view brings you 8 [self. view addSubview: self. view2]; 9 // the idea that the parent view manages the child view. In fact, the parent view should have a child view array, 10 UIView * view3 = [[UIView alloc] initWithFrame: CGRectMake (10, 10, 50, 50)]; 11 view3.backgroundColor = [UIColor orangeColor]; 12 // 2: Three insertion Methods 13 [self. view insertSubview: view3 aboveSubview: self. view1]; 14 [self. view insertSubview: view3 belowSubview: self. view2]; 15 [self. view insertSubview: view3 atIndex: 0]; 16 // 3: bring send17 [self. view bringSubviewToFront: self. view1]; 18 [self. view sendSubviewToBack: self. view2]; // operate on the parent view, put it at the bottom of the sibling view 19 // 4: Exchange 20 [self. view exchangeSubviewAtIndex: 1 withSubviewAtIndex: 0]; // switch view in sequence 21 // 5: remove 22 [view3 removeFromSuperview]; // You can go when you go, the parent view will bring you 23/6: two attributes 24 NSLog (@ "---- % @", self. view. subviews, self. view1.superview); 25 26}

 

 

 

4: Rendering

Rendering is mainly used for background color, border cutting, transparency, and display hiding.

Note:

Border cutting: border cutting is done by the parent view. The parent view is used to split the child view, which also reflects the core idea of the parent view to manage the child view.

Hide attributes: in actual development, hidden attributes can help us modify some display effects through shortcuts. In many cases, you do not need to delete these hidden attributes.

 

1/** 2*4: rendering 3 */4-(void) renderingConfig 5 {6 // 1: Background Color 7 self. view1.backgroundColor = [UIColor orangeColor]; 8 // 2: cut boundary 9 UIView * view4 = [[UIView alloc] initWithFrame: CGRectMake (10, 0, 50,200)]; 10 view4.backgroundColor = [UIColor blueColor]; 11 [self. view1 addSubview: view4]; 12 self. view1.clipsToBounds = YES; // What the parent View does, let the child View switch to its own boundary 13 // 3: Transparency 14 view4.alpha = 0.3; // The View comes with no Image15 // 4: show Hidden 16 view4.hidden = YES; // view hidden property 17 18}

 

 

5: Animation

UIView encapsulates some Animation attributes so that we can achieve a lot of Animation effects without using Core Animation. There are also a variety of implementation methods. After iOS7, the Key Frame Animation is even encapsulated. It can be seen that in development, if there is no high requirement on the animation effect, the built-in animation effect of UIView is sufficient. Here is a simple implementation of the basic animation implemented in block mode.

Note:

What is the animation changed? It is a view attribute, such as frame, backgroundColor, and alpha, and the animation effect is generally a class method. You can call it directly with UIView and write the final state of the attribute to be changed in the block.

1/** 2*5: animation 3 */4-(void) animationBlockConfig 5 {6 // first 7 [UIView animateWithDuration: 5 animations: ^ {8 self. view1.alpha = 0.1; 9}]; 10 // The second 11 [UIView animateWithDuration: 5 animations: ^ {12 self. view2.alpha = 0.1; 13} completion: ^ (BOOL finished) {14 self. view2.alpha = 1.0; 15}]; 16 // type 17 [UIView animateWithDuration: 5 delay: 6 options: UIViewAnimationOptionBeginFromCurrentState animations: ^ {18 self. view2.frame = CGRectMake (0, 0,100,100); 19 20} completion: nil]; 21 22}

 

6: gestures

Gestures mainly include adding and removing gestures.

1/** 2*6: gesture 3 */4-(void) gestureRecognizersConfig 5 {6 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; 7 // Add gesture 8 [self. view1 addGestureRecognizer: tap]; 9 // remove gesture 10 [self. view1 removeGestureRecognizer: tap]; 11 12}

 

7: Constraints

Constraints are also very important in development. There are also many ways to add constraints. There are many methods such as classes encapsulated by the system, xib tow lines, third-party libraries, and VFL languages, here are two basic methods for adding and deleting constraints to the View.

1/** 2*7: constraint 3 */4-(void) constraintConfig 5 {6 NSLayoutConstraint * constraint = [[NSLayoutConstraint alloc] init]; 7 // Add constraint 8 [self. view1 addConstraint: constraint]; 9 // Add constraint group 10 [self. view1 addConstraints: @ [constraint]; 11 // Delete constraint 12 [self. view1 removeConstraint: constraint]; 13 // Delete the constraint group 14 [self. view1 removeConstraints: @ [constraint]; 15 16}

 

 

  

Related Article

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.