6UIView子类与UIScrollView
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
// 根据bounds计算中心点
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.;
center.y = bounds.origin.y + bounds.size.height / 2.;
NSLog(@"Center: %f, %f", center.x, center.y);
// 计算圆的半径,使其大小能尽可能地撑满整个视图
float maxRadius = 0.;
if (bounds.size.width > bounds.size.height) {
maxRadius = bounds.size.height / 2.;
} else {
maxRadius = bounds.size.width / 2.;
}
// 用10点的宽度绘制所有的线条
CGContextSetLineWidth(ctx, 10);
// 将线条颜色设置为浅灰
CGContextSetRGBFillColor(ctx, 0.6, .6, .6, 1.);
// 将图形加入视图上下文-这个代码不会画出这个图形
// 画一个曲线原点,开始的角度,结束的角度,顺逆时针Bool
CGContextAddArc(ctx, center.x, 0, maxRadius, 0.0, M_PI * 2., YES)
// 执行绘图命令;根据当前的绘图状态画出上下文中的图形
// 画完图后,必须:
// 先用CGContextStrokePath来描线,即形状
// 后用CGContextFillPath来填充形状内的颜色.
CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
}
视图的背景色和drawRect:无关。通常情况下,可以将视图的背景色设置为clearColor,从而只显示drawRect:的绘图结果,通常情况下,bounds的origin会是(0,0),bounds的size会和frame的size相同,但是也有例外,所以在绘图时,切忌使用常量,而应该根据bounds的origin和size,设置绘图内容的坐标。
Core Graphics
带有CG前缀的函数和类型均源自Core Graphics框架。该框架是一套提供绘图功能的C语言API。该框架的枢纽时CGContext结构(通常会在代码中使用指向CGContext结构的指针CGContextRef):Core Graphics中的所有函数和类型,或多或少都会用到CGContext结构,并最终由上下文创建视图的图片。
CGContextAddArc的作用是将某个路径加入上下文。路径是一组点,这些点合在一起可以定义一个图形。路径可以定义任何形状的图形,例如正方形、圆形或者手绘图形。
将路径加入上下文后,还需要执行绘图操作,才能将相应的图形画出来。core Graphic提供了三个函数,可以分别用来执行不同类型的绘图操作:
CGContextStrokePath可以沿着路径画出一根线条
CGContextFillPath可以根据路径填充一个封闭的图形
CGContextClip可以定义一个裁剪区域。定义裁剪区域后,无论是哪种绘图操作,Core Graphic都只会保留裁剪区域内的绘图结果。
UIKit中的绘图扩展UIKit Drawing Additions
在Foundation框架和UIKit框架中,有很多类可以配合上下文(CGContext结构)一起工作例如:
UIColor类,可以用[[UIColor redColor]setStroke]来代替CGContextSetRGBStrokeColor函数
NSString类也提供了针对上下文的绘图功能。向NSString对象发送drawInRect:withFont:消息,可以在当前上下文的特定矩形区域中,根据指定的字体绘制当前对象所包含的字符串。
// 创建NSString对象
NSString *string = @"You are getting sleepy";
// 获取用于绘制字符串的字体
UIFont *font = [UIFont systemFontOfSize:16];
// 根据之前获取的字体,计算绘制字符串所需的矩形大小、
CGRect textRect;
textRect.size = [string sizeWithAttributes:@{NSFontAttributeName : font}];
// 要将字符串绘制在视图正中
textRect.origin.x = center.x - textRect.size.width / 2.;
textRect.origin.y = center.y - textRect.size.height / 2.;
// 将当前上下文的填充设置为黑色
[[UIColor blackColor] setFill];
// 根据字符串的绘制位置,创建阴影的偏移量,数值为向右偏4点,向下偏3点
CGSize offset = CGSizeMake(4, 3);
// 阴影的颜色是灰色
CGColorRef color = [[UIColor darkGrayColor] CGColor];
// 将以上得到的结构设置为当前上下文的阴影参数,
// 后续的绘图操作都会附带阴影效果
CGContextSetShadowWithColor(ctx, offset, 2.0, color);
// 绘制字符串
[string drawInRect:textRect withAttributes:@{NSFontAttributeName : font}];
UIImage *image = [UIImage imageNamed:@"C141F6123FBE0AC5711EADD908533A49"];
[image drawInRect:CGRectMake(20, 0, 100, 100) blendMode:kCGBlendModeHardLight alpha:0.8];
}
2 // MyView.m
3 // MyView
4 //
5 // Created by 58 on 15/9/17.
6 // Copyright © 2015年 58. All rights reserved.
7 //
8
9 #import "MyView.h"
10
11 @implementation MyView
12
13 /*
14 // Only override drawRect: if you perform custom drawing.
15 // An empty implementation adversely affects performance during animation.
16 - (void)drawRect:(CGRect)rect {
17 // Drawing code
18 }
19 */
20 - (instancetype)init{
21 self = [super init];
22 if (self) {
23 [self setBackgroundColor:[UIColor clearColor]];
24 [self setCircleColor:[UIColor lightGrayColor]];
25 }
26 return self;
27 }
28 - (void)drawRect:(CGRect)rect {
29 CGContextRef ctx = UIGraphicsGetCurrentContext();
30 CGRect bounds = [self bounds];
31
32 // 根据bounds计算中心点
33 CGPoint center;
34 center.x = bounds.origin.x + bounds.size.width / 2.;
35 center.y = bounds.origin.y + bounds.size.height / 2.;
36 NSLog(@"Center: %f, %f", center.x, center.y);
37
38 // 计算圆的半径,使其大小能尽可能地撑满整个视图
39 float maxRadius = 0.;
40 if (bounds.size.width > bounds.size.height) {
41
42 maxRadius = bounds.size.height / 2.;
43 } else {
44 maxRadius = bounds.size.width / 2.;
45 }
46
47 // 用10点的宽度绘制所有的线条
48 CGContextSetLineWidth(ctx, 1);
49
50 // 将线条颜色设置为浅灰
51 // CGContextSetRGBFillColor(ctx, 0.6, .6, 1, 1.);
52 // [[UIColor redColor] setStroke];
53 [[self circleColor]setStroke];
54
55 // 将图形加入视图上下文-这个代码不会画出这个图形
56 // 画一个曲线原点,开始的角度,结束的角度,顺逆时针Bool
57 // CGContextAddArc(ctx, center.x, center.y, maxRadius / 2, 0.0, M_PI * 2., YES);
58 // CGContextAddArc(ctx, center.x, center.y, 2, 0.0, M_PI * 2.0, YES);
59
60 // 执行绘图命令;根据当前的绘图状态画出上下文中的图形
61 // 画完图后,必须
62 // 先用CGContextStrokePath来描线,即形状
63 // 后用CGContextFillPath来填充形状内的颜色.
64 for (float currentRadius = maxRadius; currentRadius > 0 ; currentRadius -= 20) {
65 CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
66 // 执行绘图操作;移除路径
67 CGContextStrokePath(ctx);
68 NSLog(@"%f", currentRadius);
69 }
70 // CGContextStrokePath(ctx);
71 // CGContextFillPath(ctx);
72
73 // 创建NSString对象
74 NSString *string = @"You are getting sleepy";
75 // 获取用于绘制字符串的字体
76 UIFont *font = [UIFont systemFontOfSize:16];
77 // 根据之前获取的字体,计算绘制字符串所需的矩形大小、
78 CGRect textRect;
79 textRect.size = [string sizeWithAttributes:@{NSFontAttributeName : font}];
80 // 要将字符串绘制在视图正中
81 textRect.origin.x = center.x - textRect.size.width / 2.;
82 textRect.origin.y = center.y - textRect.size.height / 2.;
83 // 将当前上下文的填充设置为黑色
84 [[UIColor blackColor] setFill];
85 // 根据字符串的绘制位置,创建阴影的偏移量,数值为向右偏4点,向下偏3点
86 CGSize offset = CGSizeMake(4, 3);
87 // 阴影的颜色是灰色
88 CGColorRef color = [[UIColor darkGrayColor] CGColor];
89 // 将以上得到的结构设置为当前上下文的阴影参数,
90 // 后续的绘图操作都会附带阴影效果
91 CGContextSetShadowWithColor(ctx, offset, 2.0, color);
92 // 绘制字符串
93 [string drawInRect:textRect withAttributes:@{NSFontAttributeName : font}];
94
95 UIImage *image = [UIImage imageNamed:@"C141F6123FBE0AC5711EADD908533A49"];
96 [image drawInRect:CGRectMake(20, 0, 100, 100) blendMode:kCGBlendModeDarken alpha:0.8];
97 }
98
99 - (BOOL)canBecomeFirstResponder {
100 return YES;
101 }
102
103 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
104 if (motion == UIEventSubtypeMotionShake) {
105 NSLog(@"Device shaking");
106 [self setCircleColor:[UIColor whiteColor]];
107 [self setNeedsDisplay];
108 }
109 }
110
111 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
112
113 }
114
115 //例如用户在摇动设备时接到的电话或消息
116 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
117
118 }
119
120 @end
使用UIScrollView