There are two methods for rect in IOS we'll look at it in contrast.
Cgrectinset,cgrectoffset
Let's take a look at the official explanations.
/* Inset ' rect ' by ' (dx, dy) '--i.e., offset it origin by ' (dx, dy) ', and
Decrease its size by ' (2*DX, 2*dy) '. */
The approximate meaning is: The second parameter dx and the third parameter dy resets the first parameter rect as the result of the return. The reset is done by first translating the coordinates of the RECT (Origin) by (Dx,dy), and then reducing the size of the rect by twice times the width of the DX, and twice times the height of the dy;
Cg_extern CGRect Cgrectinset (cgrect rect, cgfloat DX, cgfloat dy)
cg_available_starting (__mac_10_0, __IPHONE_2_0);
/* Offset ' rect ' by ' (dx, dy) '. */
Approximate meaning: Rect is translated as (Dx,dy)
Cg_extern CGRect Cgrectoffset (cgrect rect,cgfloat DX, cgfloat dy)
cg_available_starting (__mac_10_0, __IPHONE_2_0);
Let's take a look at the following example
First we test Cgrectinset first create a view1 set a background color, in creating a View2 use cgrectinset to change his size according to the size of the view1 set a different color to test
The code is as follows
-(void) testcgrectinset{ UIView *view1=[[uiview alloc]initwithframe:cgrectmake (); [View1 Setbackgroundcolor:[uicolor Graycolor]];//view1 is set to gray [Self.view addsubview:view1]; Create view2 based on view1 size transform; CGRect View2rect=cgrectinset (View1.frame, a); UIView *view2=[[uiview Alloc]initwithframe:view2rect]; [View2 Setbackgroundcolor:[uicolor Bluecolor]];//view2 is set to Blue [Self.view addsubview:view2]; }
The operation results are as follows
We can see that the blue is translated first by the parameters we give and then the size is scaled.
Here we change your time to give the parameters are positive, if the negative will be what the people can go to try
Create view2 based on view1 size transform; CGRect View2rect=cgrectinset (View1.frame,-20,-20);
Down we look at the second method is the same idea
The code is as follows
-(void) testcgrectoffset{ UIView *view1=[[uiview alloc]initwithframe:cgrectmake (); [View1 Setbackgroundcolor:[uicolor Graycolor]];//view1 is set to gray [Self.view addsubview:view1]; Create view2 based on view1 size transform; CGRect View2rect=cgrectoffset (View1.frame, a); UIView *view2=[[uiview Alloc]initwithframe:view2rect]; [View2 Setbackgroundcolor:[uicolor Bluecolor]];//view2 is set to Blue [Self.view addsubview:view2]; }
Results such as
We see that compared with the previous method he only did the panning without doing size scaling
All right, two methods have been tested.
If you have any questions, you can add group discussion.
Apple Development Group: 414319235 Welcome to join the Welcome discussion question
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Cgrectinset,cgrectoffset