//
// MyQuartzView.m
// QuartzTest
//
// Created by zenny_chen on 12-2-21.
// Copyright (c) 2012年 GreenGames Studio. All rights reserved.
//
#import "MyQuartzView.h"
// Quartz2D以及Core Animation所需要的头文件
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>
@implementation MyQuartzView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if
(self) {
// Initialization code
}
return
self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (
void
)drawRect:(CGRect)rect
{
// Drawing code
// 创建Quartz上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 先填充一个alpha只为1的白色矩形
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height));
// 对于iOS坐标系,调整一下坐标系的表示,使得原点处于左下侧
// 这样与我们平时在数学中用的坐标系可取得一致
CGContextTranslateCTM(context, 0.0f, self.frame.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
// 创建一个三角Path
CGMutablePathRef path = CGPathCreateMutable();
// 调用CGPathMoveToPoint来开启一个子Path
CGPathMoveToPoint(path, &CGAffineTransformIdentity, 0.0f, self.frame.size.height);
CGPathAddLineToPoint(path, &CGAffineTransformIdentity, 0.0f, 0.0f);
CGPathAddLineToPoint(path, &CGAffineTransformIdentity, self.frame.size.width * 0.125f, 0.0f);
CGPathAddLineToPoint(path, &CGAffineTransformIdentity, 0.0f, self.frame.size.height);
CGPathCloseSubpath(path);
// 设置Path的混合模式:
// kCGBlendModeDestinationIn表示:如果alpha为0,那么采用目标像素
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
// 这里主要设置该path的alpha值为0
CGContextSetRGBFillColor(context, 0.0f, 0.0f, 0.0f, 0.0f);
// 添加Path并绘制该Path
CGContextAddPath(context, path);
CGContextFillPath(context);
CGPathRelease(path);
}
@end
|
然后,我们再看看主控制器里面的代码:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
//
// ViewController.m
// QuartzTest
//
// Created by zenny_chen on 12-2-21.
// Copyright (c) 2012年 GreenGames Studio. All rights reserved.
//
#import "ViewController.h"
#import "MyQuartzView.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
- (
void
)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 设置主视图的背景色
self.view.backgroundColor = [UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f];
// 创建一个红色背景的矩形图
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 160.0f, 160.0f)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
[aView release];
// 创建掩模视图,其尺寸与所要裁减的视图的尺寸一样
MyQuartzView *myView = [[MyQuartzView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 160.0f, 160.0f)];
// 这里要注意的是,必须将掩模视图的背景色的alpha值填充为0
myView.backgroundColor = [UIColor clearColor];
// 将掩模视图的layer作为被裁减视图的layer的mask
aView.layer.mask = myView.layer;
// 注意,这里的myView不能调release方法,
// 因为aView.layer.mask = myView.layer这句并没有将myView给retain住。
}
- (
void
)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (
BOOL
)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return
(interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
|