iOS7 CGRectInset 该结构体的应用

1
2
3
4
5
CGRect CGRectInset (
CGRect rect,
CGFloat dx,
CGFloat dy
);

该结构体的应用是以原rect为中心,再参考dx,dy,进行缩放或者放大。

我们做一个示例来看看是不是这样的:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect currentRect = self.view.frame;
CGContextAddRect(context, currentRect);
CGContextDrawPath(context, kCGPathFillStroke);
CGRect original = CGRectMake(100, 100, 200, 200);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextAddRect(context, original);
CGContextDrawPath(context, kCGPathFillStroke);

CGRect firstRect = CGRectInset(original, 10, 10);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextAddRect(context, firstRect);
CGContextDrawPath(context, kCGPathFillStroke);

CGRect secondRect = CGRectInset(firstRect, 10, 10);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextAddRect(context, secondRect);
CGContextDrawPath(context, kCGPathFillStroke);

CGRect thirdRect = CGRectInset(secondRect, 10, 10);
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextAddRect(context, thirdRect);
CGContextDrawPath(context, kCGPathFillStroke);

CGRect fourRect = CGRectInset(thirdRect, 10, 10);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextAddRect(context, fourRect);


UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:destImg];
[self.view addSubview:imgView];
}

从这个示例可以看出其使用方法是如何使用的。