矢量图形

    我们用Core Graphics来绘图的一个通常原因就是只是用图片或是图层效果不能轻易地绘制出矢量图形。矢量绘图包含一下这些:

  • 任意多边形(不仅仅是一个矩形)
  • 斜线或曲线
  • 文本
  • 渐变

    举个例子,清单13.1 展示了一个基本的画线应用。这个应用将用户的触摸手势转换成一个UIBezierPath上的点,然后绘制成视图。我们在一个UIView子类DrawingView中实现了所有的绘制逻辑,这个情况下我们没有用上view controller。但是如果你喜欢你可以在view controller中实现触摸事件处理。图13.1是代码运行结果。

清单13.1 用Core Graphics实现一个简单的绘图应用

  1. #import "DrawingView.h"
  2. @interface DrawingView ()
  3. @property (nonatomic, strong) UIBezierPath *path;
  4. @end
  5. @implementation DrawingView
  6. - (void)awakeFromNib
  7. {
  8. //create a mutable path
  9. self.path = [[UIBezierPath alloc] init];
  10. self.path.lineJoinStyle = kCGLineJoinRound;
  11. self.path.lineCapStyle = kCGLineCapRound;
  12. self.path.lineWidth = 5;
  13. }
  14. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  15. {
  16. //get the starting point
  17. CGPoint point = [[touches anyObject] locationInView:self];
  18. //move the path drawing cursor to the starting point
  19. [self.path moveToPoint:point];
  20. }
  21. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  22. {
  23. //get the current point
  24. CGPoint point = [[touches anyObject] locationInView:self];
  25. //add a new line segment to our path
  26. [self.path addLineToPoint:point];
  27. //redraw the view
  28. [self setNeedsDisplay];
  29. }
  30. - (void)drawRect:(CGRect)rect
  31. {
  32. //draw path
  33. [[UIColor clearColor] setFill];
  34. [[UIColor redColor] setStroke];
  35. [self.path stroke];
  36. }
  37. @end

图13.1

图13.1 用Core Graphics做一个简单的『素描』

    这样实现的问题在于,我们画得越多,程序就会越慢。因为每次移动手指的时候都会重绘整个贝塞尔路径(UIBezierPath),随着路径越来越复杂,每次重绘的工作就会增加,直接导致了帧数的下降。看来我们需要一个更好的方法了。

    Core Animation为这些图形类型的绘制提供了专门的类,并给他们提供硬件支持(第六章『专有图层』有详细提到)。CAShapeLayer可以绘制多边形,直线和曲线。CATextLayer可以绘制文本。CAGradientLayer用来绘制渐变。这些总体上都比Core Graphics更快,同时他们也避免了创造一个寄宿图。

    如果稍微将之前的代码变动一下,用CAShapeLayer替代Core Graphics,性能就会得到提高(见清单13.2).虽然随着路径复杂性的增加,绘制性能依然会下降,但是只有当非常非常浮躁的绘制时才会感到明显的帧率差异。

清单13.2 用CAShapeLayer重新实现绘图应用

  1. #import "DrawingView.h"
  2. #import <QuartzCore/QuartzCore.h>
  3. @interface DrawingView ()
  4. @property (nonatomic, strong) UIBezierPath *path;
  5. @end
  6. @implementation DrawingView
  7. + (Class)layerClass
  8. {
  9. //this makes our view create a CAShapeLayer
  10. //instead of a CALayer for its backing layer
  11. return [CAShapeLayer class];
  12. }
  13. - (void)awakeFromNib
  14. {
  15. //create a mutable path
  16. self.path = [[UIBezierPath alloc] init];
  17. //configure the layer
  18. CAShapeLayer *shapeLayer = (CAShapeLayer *)self.layer;
  19. shapeLayer.strokeColor = [UIColor redColor].CGColor;
  20. shapeLayer.fillColor = [UIColor clearColor].CGColor;
  21. shapeLayer.lineJoin = kCALineJoinRound;
  22. shapeLayer.lineCap = kCALineCapRound;
  23. shapeLayer.lineWidth = 5;
  24. }
  25. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  26. {
  27. //get the starting point
  28. CGPoint point = [[touches anyObject] locationInView:self];
  29. //move the path drawing cursor to the starting point
  30. [self.path moveToPoint:point];
  31. }
  32. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  33. {
  34. //get the current point
  35. CGPoint point = [[touches anyObject] locationInView:self];
  36. //add a new line segment to our path
  37. [self.path addLineToPoint:point];
  38. //update the layer with a copy of the path
  39. ((CAShapeLayer *)self.layer).path = self.path.CGPath;
  40. }
  41. @end