您的位置:首页 > 其它

关于Quartz2D的几个demo

2016-01-31 21:49 489 查看

画饼图



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect {
// Drawing code

NSArray *data = @[@25,@25,@50];

// 圆心
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat startA = 0;
CGFloat endA = 0;
CGFloat angle = 0;

for (NSNumber *num in data) {
startA = endA;
angle = [num integerValue] / 100.0 * M_PI * 2;
endA = startA + angle;
// 画第一个扇形
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:startA endAngle:endA clockwise:YES];

// 连接一根线到圆心
[path addLineToPoint:center];
//set:表示填充和描边的颜色
[[self randomColor] set];

[path fill];

}

}
// 随机颜色
- (UIColor *)randomColor
{
CGFloat r = arc4random_uniform(256) / 255.0;
CGFloat g = arc4random_uniform(256) / 255.0;
CGFloat b = arc4random_uniform(256) / 255.0;

return [UIColor colorWithRed:r green:g blue:b alpha:1];
}


模仿下载进度



@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelView;
@property (weak, nonatomic) IBOutlet ProgressView *progressView;

@end

@implementation ViewController
- (IBAction)valueChange:(UISlider *)sender {
// %% = %
_labelView.text = [NSString stringWithFormat:@"%.2f%%",sender.value * 100];

// 传入下载进度
_progressView.progress = sender.value;
}

@implementation ProgressView

- (void)setProgress:(CGFloat)progress
{
_progress = progress;

// 重绘
// drawRect不能手动调用,
// drawRect只能系统调用,每次系统调用drawRect方法之前,都会给drawRect方法传递一个跟当前view相关联上下文
[self setNeedsDisplay];
//    [self drawRect:self.bounds];
}

// drawRect默认只会调用一次
- (void)drawRect:(CGRect)rect {

NSLog(@"%s",__func__);
// Drawing code
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);

CGFloat endA = - M_PI_2 + M_PI * 2 * _progress;

// 圆弧
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:rect.size.width * 0.5 - 4 startAngle:-M_PI_2 endAngle:endA clockwise:YES];

[path stroke];

}


绘制文字

// 绘制文字
- (void)drawText
{
// 文字
NSString *str = @"hello world hello world hello world";

// Attributes:给文字添加属性,富文本,字体,颜色,空心,阴影
// 用一个字典去描述文本属性
NSMutableDictionary *strAttr = [NSMutableDictionary dictionary];
// 设置字典,描述文本,字典内容 是 一个key对应一个value
// 查看描述文本的key
// 字体
strAttr[NSFontAttributeName] = [UIFont systemFontOfSize:50];
// 颜色
strAttr[NSForegroundColorAttributeName] = [UIColor redColor];

// 描边
strAttr[NSStrokeWidthAttributeName] = @1;
//    strAttr[NSStrokeColorAttributeName] = [UIColor greenColor];

// 阴影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(10, 10);
shadow.shadowColor = [UIColor yellowColor];
shadow.shadowBlurRadius = 5;
strAttr[NSShadowAttributeName] = shadow;

// drawAtPoint不会自动换行
//    [str drawAtPoint:CGPointZero withAttributes:strAttr];

// drawInRect可以自动换行
[str drawInRect:self.bounds withAttributes:strAttr];
// 1.获取上下文 2.描述路径 3.把路径添加到上下文 4.渲染上下文

}


绘制图片

- (void)drawRect:(CGRect)rect {
// 绘制图片
UIImage *image = [UIImage imageNamed:@"阿狸头像"];
// 超出控件的部分会被裁剪掉
// drawAtPoint:默认绘制的图片跟图片本身一样
//    [image drawAtPoint:CGPointZero];

// drawInRect:默认绘制的图片跟控件一样,拉伸跟控件一样大
[image drawInRect:rect];

// 设置裁剪区域的代码必须要在绘制之前
// 设置裁剪区域,超出裁剪区域的部分会被裁剪掉
//    UIRectClip(CGRectMake(0, 0, 50, 50));
// Pattern:平铺
//    [image drawAsPatternInRect:rect];

// 快速的填充一个矩形
//    UIRectFill(CGRectMake(0, 0, 50, 50));

}


旋转,平移,缩放

- (void)drawRect:(CGRect)rect {
// Drawing code

// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

// 给上下文做形变
// 平移
CGContextTranslateCTM(ctx, 100, 100);

// 旋转
CGContextRotateCTM(ctx, M_PI_4);

// 缩放
CGContextScaleCTM(ctx, 0.5, 0.5);

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-50, -100, 100, 200)];

[[UIColor redColor] set];

[path fill];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: