一. Quartz2D的绘制不同图形

1. 绘图步骤

1> 自定义一个View

2> 在- (void)drawrectangle方法中进行绘图

  • 获取当前上下文

  1. CGContextRef ref = UIGraphicsGetCurrentContext();
  • 绘制路径
  1. UIBezierPath *path = [UIBezierPath bezierPath];
  • 保存路径到上下文种
  1. CGContextAddPath(ref, path.CGPath);
  • 将上下文中的内容渲染到view的layer上(描边:stroke 填充:fill)
  1. CGContextStrokePath(ref);

2. 绘制直线

  1. - (void)drawreLine
  2. {
  3. // 1.获取上下文
  4. CGContextRef ref = UIGraphicsGetCurrentContext();
  5. // 2.绘制路径
  6. UIBezierPath *path = [UIBezierPath bezierPath];
  7. // 2.1 设置起点
  8. [path moveToPoint:CGPointMake(20, 250)];
  9. // 2.2 设置线的终点
  10. [path addLineToPoint:CGPointMake(100, 50)];
  11. // 3.保存路径到上下文中
  12. CGContextAddPath(ref, path.CGPath);
  13. // 4.将上下文中的内容渲染到view上
  14. CGContextStrokePath(ref);
  15. }

3. 绘制矩形框

  1. - (void)drawrectangle
  2. {
  3. // 1.获取上下文
  4. CGContextRef ref = UIGraphicsGetCurrentContext();
  5. // 2.绘制路径
  6. UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 50, 60)];
  7. // 3.保存路径到上下文
  8. CGContextAddPath(ref, path.CGPath);
  9. [[UIColor redColor] set];
  10. // 4.上下文渲染到view上
  11. CGContextStrokePath(ref);
  12. }

4. 绘制圆

  1. - (void)drawCirle
  2. {
  3. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 50, 40) cornerRadius:20];
  4. [path stroke];
  5. }

5. 绘制圆弧

  1. - (void)drawRect:(CGRect)rect {
  2. CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
  3. CGFloat radius = self.bounds.size.width * 0.5 - 10;
  4. CGFloat startAngle = -M_PI_2;
  5. CGFloat endAngle = startAngle + self.progress * M_PI * 2;
  6. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  7. [path stroke];
  8. }

6. 绘制饼型

  1. - (void)drawRect:(CGRect)rect {
  2. NSArray *dataArray = @[@25, @25, @50];
  3. CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
  4. CGFloat radius = self.bounds.size.width * 0.5 - 10;
  5. CGFloat startA = 0;
  6. CGFloat angle = 0;
  7. CGFloat endA = 0;
  8. for (NSNumber *num in dataArray) {
  9. startA = endA;
  10. angle = num.intValue / 100.0 * M_PI * 2;
  11. endA = startA + angle;
  12. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
  13. [[self randomColor] set];
  14. [path addLineToPoint:center];
  15. [path fill];
  16. }
  17. }
  18. // 随机颜色
  19. - (UIColor *)randomColor
  20. {
  21. CGFloat R = arc4random_uniform(256) / 255.0;
  22. CGFloat G = arc4random_uniform(256) / 255.0;
  23. CGFloat B = arc4random_uniform(256) / 255.0;
  24. return [UIColor colorWithRed:R green:G blue:B alpha:1];
  25. }
  26. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  27. {
  28. // 重绘
  29. [self setNeedsDisplay];
  30. }

二. Quartz2D的一些基本属性

1. 绘文字

  1. - (void)drawRect:(CGRect)rect {
  2. // Drawing code
  3. //画文字,无论有没有看到上下文,只要是绘制东西到View上,就必须得要在drawRect方法当中进行.
  4. NSString *str = @"adsfasffasdfasdfasdfa";
  5. //withAttributes,设文字的属性.,文字大小,颜色,阴影,描边.x
  6. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  7. //key,value,
  8. dict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
  9. dict[NSForegroundColorAttributeName] = [UIColor redColor];
  10. //设置阴影
  11. NSShadow *shdow = [[NSShadow alloc] init];
  12. shdow.shadowOffset = CGSizeMake(10, -10);
  13. shdow.shadowColor = [UIColor greenColor];
  14. shdow.shadowBlurRadius = 3;
  15. dict[NSShadowAttributeName] = shdow;
  16. //设置描边
  17. dict[NSStrokeWidthAttributeName] = @2;
  18. //设置描边颜色
  19. dict[NSStrokeColorAttributeName] = [UIColor blueColor];
  20. //drawAtPoint:不会自动换行
  21. //drawInRect:会自动换行.
  22. //画文字
  23. [str drawAtPoint:CGPointZero withAttributes:dict];
  24. [str drawInRect:rect withAttributes:dict];
  25. }

2. 绘图片

  1. - (void)drawRect:(CGRect)rect {
  2. // Drawing code
  3. //画图片.
  4. //加载图片
  5. UIImage *image = [UIImage imageNamed:@"001"];
  6. //绘制图片是原图片的尺寸大小.
  7. // [image drawAtPoint:CGPointZero];
  8. //把整张图片填充到rect这个区域当中.
  9. // [image drawInRect:rect];
  10. // //平铺
  11. [image drawAsPatternInRect:rect];
  12. // //裁剪,超过裁剪区域的内容,都会被裁剪掉.,设置裁剪区域,必须得要在绘制图片之前.
  13. // UIRectClip(CGRectMake(0, 0, 50, 50));
  14. //
  15. // [image drawAtPoint:CGPointZero];
  16. //快速的填充一个矩形.
  17. UIRectFill(CGRectMake(0, 100, 100, 100));
  18. }

3. 雪花(定时器)

  1. - (void)awakeFromNib
  2. {
  3. // 添加定时器
  4. CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImageY)];
  5. [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  6. }
  7. - (void)updateImageY
  8. {
  9. _imageY += 10;
  10. if (_imageY > self.bounds.size.height) {
  11. _imageY = 0;
  12. }
  13. [self setNeedsDisplay];
  14. }
  15. - (void)drawRect:(CGRect)rect {
  16. // 1.加载图片
  17. UIImage *image = [UIImage imageNamed:@"雪花"];
  18. [image drawAtPoint:CGPointMake(0, _imageY)];
  19. }

4. 水印

  1. // 1.水印
  2. - (void)watermark
  3. {
  4. // 0.加载图片
  5. UIImage *image = [UIImage imageNamed:@"小黄人"];
  6. // 1.开启位图上下文
  7. UIGraphicsBeginImageContext(image.size);
  8. // 2.绘制图片
  9. [image drawAtPoint:CGPointZero];
  10. // 3.绘制文字
  11. NSString *str = @"小码哥";
  12. NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
  13. dictM[NSFontAttributeName] = [UIFont systemFontOfSize:50];
  14. dictM[NSForegroundColorAttributeName] = [UIColor redColor];
  15. [str drawAtPoint:CGPointZero withAttributes:dictM];
  16. // 4.生成新的图片
  17. image = UIGraphicsGetImageFromCurrentImageContext();
  18. // 5.销毁位图上下文
  19. UIGraphicsEndImageContext();
  20. self.watermarkImageView.image = image;
  21. }

5. 截取圆形图片

  1. // 2.截取圆形图片
  2. - (void)circle
  3. {
  4. // 0.加载图片
  5. UIImage *image = [UIImage imageNamed:@"阿狸头像"];
  6. // 1.开启位图上下文
  7. UIGraphicsBeginImageContext(image.size);
  8. // 2.设置裁剪区域
  9. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  10. [path addClip];
  11. // 3.绘制图片
  12. [image drawAtPoint:CGPointZero];
  13. // 4.生成新的图片
  14. image = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();
  16. self.circleImageView.image = image;
  17. }

6.截取带有边框的圆形图片

  1. // 3.截取有边框的圆形图片
  2. - (void)borderCircle
  3. {
  4. // 截取带有边框的圆形图片
  5. CGFloat border = 10;
  6. // 0.加载图片
  7. UIImage *image = [UIImage imageNamed:@"阿狸头像"];
  8. // 1.开启位图上下文
  9. CGSize bitmapSize = CGSizeMake(image.size.width + border * 2, image.size.height + border * 2);
  10. UIGraphicsBeginImageContext(bitmapSize);
  11. // 2.绘制大的圆形
  12. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, bitmapSize.width, bitmapSize.height)];
  13. [[UIColor redColor] set];
  14. [path fill];
  15. // 3.设置小圆裁剪区域
  16. path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, image.size.width, image.size.height)];
  17. [path addClip];
  18. // 4.绘制图片
  19. [image drawAtPoint:CGPointMake(border, border)];
  20. // 5.生成新的图片
  21. image = UIGraphicsGetImageFromCurrentImageContext();
  22. // 6.关闭位图上下文
  23. UIGraphicsEndImageContext();
  24. self.circleImageView.image = image;
  25. }

7. 截屏

  1. // 截屏
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  3. {
  4. // 1.开启位图上下文
  5. UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
  6. // 2.获取位图上下文
  7. CGContextRef crf = UIGraphicsGetCurrentContext();
  8. // 3.将屏幕view的layer渲染到路径中
  9. [self.view.layer renderInContext:crf];
  10. // 4.获取位图上下文中的内容
  11. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  12. // 5.关闭位图上下文
  13. UIGraphicsEndImageContext();
  14. NSData *data = UIImagePNGRepresentation(image);
  15. [data writeToFile:@"/Users/admin/Desktop/image.png" atomically:YES];
  16. }

8. 移动鼠标确定截取屏幕的大小

1> 定义一个手势

  1. - (void)pan:(UIPanGestureRecognizer *)pan
  2. {
  3. CGPoint curP = [pan locationInView:self.huoyingImageView];
  4. if (pan.state == UIGestureRecognizerStateBegan) { // 开始拖动
  5. self.startP = curP;
  6. } else if (pan.state == UIGestureRecognizerStateChanged) { // 拖动中
  7. CGFloat coverVX = self.startP.x;
  8. CGFloat coverVY = self.startP.y;
  9. CGFloat coverVW = curP.x - self.startP.x;
  10. CGFloat coverVH = curP.y - self.startP.y;
  11. CGRect frame = CGRectMake(coverVX, coverVY, coverVW, coverVH);
  12. self.coverV.frame = frame;
  13. } else if (pan.state == UIGestureRecognizerStateEnded) { // 结束拖动
  14. // 1.开启位图上下文
  15. UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
  16. // 2.设置裁剪区域
  17. UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverV.frame];
  18. [path addClip];
  19. // 3.获取当前位图上下文
  20. CGContextRef ref = UIGraphicsGetCurrentContext();
  21. // 4.将图片的layer渲染到上下文
  22. [self.huoyingImageView.layer renderInContext:ref];
  23. // 5.生成一张新的图片并赋值给huoyingImageView
  24. self.huoyingImageView.image = UIGraphicsGetImageFromCurrentImageContext();
  25. // 6.关闭位图上下文
  26. UIGraphicsEndImageContext();
  27. // 7.移除遮盖
  28. [self.coverV removeFromSuperview];
  29. }
  30. }

2> 设置遮盖View

  1. #pragma mark - View懒加载
  2. - (UIView *)coverV
  3. {
  4. if (!_coverV) {
  5. UIView *coverV = [[UIView alloc] init];
  6. coverV.backgroundColor = [UIColor blackColor];
  7. coverV.alpha = 0.7;
  8. [self.view addSubview:coverV];
  9. self.coverV = coverV;
  10. }
  11. return _coverV;
  12. }

9. 撕图效果

设置清除区域  CGContextClearRect(ref, rect);

  1. - (void)pan:(UIPanGestureRecognizer *)pan
  2. {
  3. CGPoint curP = [pan locationInView:self.imageView];
  4. // 1.开启位图上下文
  5. UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);
  6. // 2.获取当前位图上下文
  7. CGContextRef ref = UIGraphicsGetCurrentContext();
  8. // 3.将图片渲染到位图上下文种
  9. [self.imageView.layer renderInContext:ref];
  10. // 4.设置清除区域
  11. CGFloat WH = 30;
  12. CGFloat X = curP.x - WH * 0.5;
  13. CGFloat Y = curP.y - WH * 0.5;
  14. CGRect rect = CGRectMake(X, Y, WH, WH);
  15. CGContextClearRect(ref, rect);
  16. // 5.获取位图上下文种的图片并赋值给imageView
  17. self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
  18. // 6.关闭位图上下文
  19. UIGraphicsEndImageContext();
  20. }

转载于:https://www.cnblogs.com/Xfsrn/p/5000340.html

iOS开发核心动画之Quartz2D绘图相关推荐

  1. iOS开发核心动画之画图板

    1. 效果图 2. 用一个View来描述画图板,给画图板添加拖动的手势 // 从xib中加载 - (void)awakeFromNib { [self setUpGesture]; } // 代码创建 ...

  2. iOS学习——核心动画之Layer基础

    iOS学习--核心动画之Layer基础 1.CALayer是什么? CALayer我们又称它叫做层.在每个UIView内部都有一个layer这样一个属性,UIView之所以能够显示,就是因为它里面有这 ...

  3. iOS 开发之动画篇 - 从 UIView 动画说起

    毋庸置疑的:在iOS开发中,制作动画效果是最让开发者享受的环节之一.一个设计严谨.精细的动画效果能给用户耳目一新的效果,吸引他们的眼光 -- 这对于app而言是非常重要的. 本文作为动画文集的第一篇, ...

  4. iOS开发--Core Graphics绘图

    一. Core Graphics简介 Core Graphics是一个基于C的绘图专用的API族,它经常被称为QuartZ或QuartZ 2D,是一个二维绘图引擎,同时支持iOS和Mac系统.它提供了 ...

  5. iOS CoreAnimation 核心动画系列博客索引

    学了这些文章能做什么,了解核心动画各个类能做什么,产品有些简单的视图平移.旋转.折叠.抖动的需求,都能搞了 我们写博客的目的是学习研究之后做记录,捎带让别人指教,就是这么简单 博客文章都是对同一个领域 ...

  6. iOS开发-动画总结

    一.简介 IOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...

  7. ios开发学习--动画(Animation)效果源码分享--系列教程1

    Genie View        介绍: 实现所谓的genie effect.即点击最小化或删除按钮,视图会被吸进某个地方.         http://ios.itmdc.com/forum.p ...

  8. iOS 开发之动画篇 - Transform和KeyFrame动画

    原文发布于http://www.jianshu.com/p/a071bba99a1b 序言 追求美好是人的天性,这是猿们无法避免的.我们总是追求更为酷炫的实现,如果足够仔细,我们不难发现一个好的动画通 ...

  9. IOS开发之——动画-转场动画(98)

    一 概述 CATransition转场动画介绍 专场动画属性及过渡效果 示例(动画切换模拟转场动画) 二 CATransition专场动画介绍 nCATransition是CAAnimation的子类 ...

最新文章

  1. 使用NATAPP.cn测试微信支付回调接口
  2. Linux常用的文本查找命令 find
  3. Firefox 控制台
  4. 面试:说说你对 HashMap 的认识?
  5. MIT Molecular Biology 笔记5 转录机制
  6. Python的进程和线程(二)——IO密集型任务
  7. [浪风推荐]php的memcache应用入门教程
  8. java nio有哪些功能_如何真正理解java中的NIO?
  9. mysql 压缩的blob不能正常显示中文内容_servlet网页显示MySQL BLOB中文乱码
  10. SpringCloud工作笔记053---SLF4J简介与使用(整合log4j_并切换logging)
  11. Spring Boot从Controller层进行单元测试
  12. 拓端tecdat|R语言估计获胜概率:模拟分析学生多项选择考试通过概率可视化
  13. 西威变频器avo下载调试资料_步骤详解|西门子PLCS7-1200通过周期性通讯PZD通道时组态和下载...
  14. 东原罗韶颖:城市深耕中的社区商业逻辑
  15. 使用stata临床决策曲线进行外部模型验证
  16. android如何怎么禁止多点触控
  17. Centos 7 grub 菜单加密以及取消加密
  18. 致批驳哥德尔不完全性定理者
  19. N个实用的css代码
  20. Unity调用Window提示框Yes/No(英文提示窗)

热门文章

  1. 文字样式的常见属性的如何使用?
  2. signalr for java_ASP.NET Core SignalR Java 客户端
  3. 深度学习100例 | 第36天:FMD材料识别
  4. 查看linux多少位操作系统,linux系统查看是多少位的命令
  5. 基于Overleaf的pdf修改前后对比软件安装
  6. 数据库设计规范之三大范式
  7. Linux网络编程之实现服务器与客户端之间的通讯
  8. chroot运行完整linux发行版,chroot
  9. 基于函数计算快速实现《为你写诗》(阿里云ECS)
  10. GRU:什么是GRU?为什么要学习GRU?