CGContext vs CALayer
Ich versuche Quarz 2D in den Griff zu bekommen und schaue mir das Online-Buch anQuartz 2D-Grafik für Mac OS X-Entwickler.
Eine sehr grundlegende Sache, die mich verwirrt, ist der CGContext. Zum Beispiel kann ich mit dem folgenden Code eine einfache "U" -Form zeichnen und sie in a verwendenCAShapeLayer
ohne zu referenzieren aCGContext
. Wird der Kontext standardmäßig durch die impliziert / bereitgestellt?CAShapeLayer
?
Ich vermische hier wahrscheinlich mehrere iOS / OSX-Grafik-APIs, damit vielleicht jemand klarstellen kann, wo ich falsch liege.
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];