我正在尝试了解 Quartz 2D 并查看在线书籍Quartz 2D Graphics for Mac OS X Developers。
让我感到困惑的一件非常基本的事情是 CGContext。例如,我可以使用下面的代码绘制一个简单的“U”形,并在 a 中使用它CAShapeLayer
而不引用 a CGContext
。默认情况下是否隐含/提供上下文CAShapeLayer
?
我可能在这里混合了几个 iOS/OSX 图形 API,所以也许有人可以澄清我哪里出错了。
CGPoint pt1 = CGPointMake(100, 100);
CGPoint pt2 = CGPointMake(150, 100);
CGPoint pt3 = CGPointMake(150, 200);
CGPoint pt4 = CGPointMake(190, 200);
CGPoint pt5 = CGPointMake(190, 100);
CGPoint pt6 = CGPointMake(210, 100);
CGPoint pt7 = CGPointMake(210, 250);
CGPoint pt8 = CGPointMake(100, 250);
CGPoint pt9 = CGPointMake(100, 100);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, pt1.x,pt1.y);
CGPathAddLineToPoint(path, nil, pt2.x, pt2.y);
CGPathAddLineToPoint(path, nil, pt3.x, pt3.y);
CGPathAddLineToPoint(path, nil, pt4.x, pt4.y);
CGPathAddLineToPoint(path, nil, pt5.x, pt5.y);
CGPathAddLineToPoint(path, nil, pt6.x, pt6.y);
CGPathAddLineToPoint(path, nil, pt7.x, pt7.y);
CGPathAddLineToPoint(path, nil, pt8.x, pt8.y);
CGPathAddLineToPoint(path, nil, pt9.x, pt9.y);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[self.view.layer addSublayer:shapeLayer];