关于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]; }
相关文章推荐
- Spark Streaming学习与实践(1)
- HDOJ 2058 The sum problem
- HDOJ 2058 The sum problem
- 如何在2016年成为一个更好的Node.js开发者
- redis集群
- hdu 2123 An easy problem
- hdoj4810Wall Painting【组合数学+位运算】
- mysql数据库——DBA
- CGContextSaveGState与CGContextRestoreGState的作用
- Velocity知识点总结
- POJ3070-Fibonacci-矩阵快速幂
- HDU Wolf and Rabbit &&189 兔子的烦恼(一)【gcd】
- 电力猫
- 浅谈printf函数的返回值
- 自动加载类
- 电力猫
- 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败
- python在不同层级目录import模块的方法
- 获取当前应用的版本名称和版本号
- Queue之ArrayDeque源码