IOS development-UIView of the UIKit control (View) and uikituiview
(Note: This article only records the things I usually need as a memo)
UIView is the most common and commonly used control in iOS development. It is a control encapsulated in the UIKit framework. It is powerful and practical!
UIView is inherited from UIResponder. To use UIView, You need to import the main header file <UIKit/UIKit. h>. Generally, when you create a project or create a view, X-code will help you import this header file by default.
Create UIView
1 // create UIView 2 UIView * view = [[UIView alloc] init]; 3 // Set coordinates and dimensions 4/** 5 * CGRect 6*1. x: X axis coordinate value 7*2. y: Y axis coordinate value 8*3. width: 9*4 in Width. height: height 10 */11 [view setFrame: CGRectMake (110,234,100,100)]; 12 // It can also be abbreviated as 13 view. frame = CGRectMake (110,234,100,100); 14 // This step combines the code in the previous two steps (creating a View and setting a Frame) 15 UIView * view = [[UIView alloc] initWithFrame: CGRectMake (110,234,100,100)]; 16 // Add this View to the interface so that we can see it 17 [self. view addSubview: view]; 18 // However, we still cannot see it because it is white by default, by default, our interface is also white 19 // we need to modify one of the colors to see 20 // set the background color of the View lightGrayColor (bright gray) 21 [view setBackgroundColor: [UIColor lightGrayColor];
Run the program as follows:
Attribute
Now you can see the View, and set the attributes (in fact, Frame and BackgroundColor are also attributes of the View)
1 // Coordinate Position 2 struct CGPoint {3 CGFloat x; 4 CGFloat y; 5}; 6 typedef struct CGPoint; 7 8 // size 9 10 struct CGSize {11 CGFloat width; 12 CGFloat height; 13}; 14 typedef struct CGSize; 15 16 // region range (including coordinates and dimensions) 17 struct CGRect {18 CGPoint origin; 19 CGSize size; 20}; 21 typedef struct CGRect; 22 23 24 25 CGRect rect = CGRectMake (320,480 ); 26 UIView * view = [[UIView allow] initWithFrame: rect]; 27 28 // convert String to CGPoint such as @ "{3.0, 2.5}" {x, y} 29 CGPoint CGPointFromString (30 NSString * string 31); 32 33 // convert String to CGRect @ "{3, 2}, {4, 5}" {x, y }, {w, h }}34 CGRect CGRectFromString (35 NSString * string 36); 37 38 // convert String to CGSize @ "{3.0, 2.5}" {w, h} 39 CGSize CGSizeFromString (40 NSString * string 41); 42 43 // convert CGPoint to NSString 44 NSString * NSStringFromCGPoint (45 CGPoint point 46 ); 47 48 // convert CGRect to NSString 49 NSString * NSStringFromCGRect (50 CGRect rect 51); 52 53 // convert CGSize to NSString 54 NSString * NSStringFromCGSize (55 CGSize 56 ); 57 58 // modify a CGRect. In this center, modify positive numbers to indicate smaller (smaller) negative numbers to indicate greater (enlarged) 59 CGRect CGRectInset (60 CGRect rect, 61 CGFloat dx, 62 CGFloat dy 63); 64 65 // determine whether two rectangles intersect. 66 bool CGRectIntersectsRect (67 CGRect rect1, 68 CGRect rect2 69 ); 70 71 // 72 const CGPoint CGPointZero; 73 const CGRect CGRectZero; 74 const CGSize CGSizeZero; 75 76 // create CGPoint 77 CGPoint CGPointMake (78 CGFloat x, 79 CGFloat y 80); 81 // create CGRect 82 CGRect CGRectMake (83 CGFloat x, 84 CGFloat y, 85 CGFloat width, 86 CGFloat height 87 ); 88 // create CGSize 89 CGSize CGSizeMake (90 CGFloat width, 91 CGFloat height 92 );
(Only a part of the List is listed here. I can only think of it for the time being, and I will update it later)
1 // attribute 2 // set the tag value 3 view. tag = 0; 4 // set the center point 5 view. center = CGPointMake (160,284); 6 // set display/hide (NO is displayed, YES is hidden) 7 view. hidden = NO; 8 // set the rounded corner (cornerRadius: rounded corner radius) 9 view. layer. cornerRadius = 10.0f; 10 // you can specify the part that has been cut out. layer. masksToBounds = YES; 12 // set the Border width to 13 view. layer. borderWidth = 1.0f; 14 // set the border color (purpleColor is purple) 15 // borderColor: only the C color is accepted, so we need to convert it to CGColor16 view. layer. borderColor = [UIColor purpleColor]. CGColor;
Run:
Method
In fact, the addSubview we used previously is a View method (the function is to add a View to another View)
1 // move a View to the front 2 [UIView bringSubviewToFront:]; 3 // push a View to the back 4 [UIView sendSubviewToBack:] 5 // remove view 6 [UIView removeFromSuperview] 7 // insert View and specify index 8 [UIView insertSubview: atIndex:] 9 // insert a view on top 10 of a view [UIView insertSubview: aboveSubview:] 11 // insert a view under a view 12 [UIView insertSubview: belowSubview:] 13 // switch the two-Position Index view 14 [UIView exchangeSubviewAtIndex: withSubviewAtIndex:]
Animation
In fact, animation is also one of the View methods, but this function is more common and practical, so I opened a separate.
The animation code is executed within the animation duration,
For example, if we directly set the Frame for translation, we will see that it has passed in a flash. However, in the animation method, we can see that it has moved slowly. This moving time is our animation duration.
1 // Animation 2 // Duration: animation Duration 3 // animations: animation effect code 4 [UIView animateWithDuration: 0.3f animations: ^ {5 // animation Code 6}];