iOS开发核心动画之Quartz2D绘图

一. 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. }


posted @ 2015-11-27 12:51  文刂Rn  阅读(390)  评论(0编辑  收藏  举报