CAShapeLayer用于绘制带有矩形的三角形

时间:2016-01-06 10:29:32

标签: ios objective-c iphone cashapelayer

我想像这个图像一样绘制带有矩形的三角形。

enter image description here

但我可以使用此代码绘制这样的形状。 enter image description here

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = imgTest.layer.bounds;
CGFloat width = imgTest.layer.frame.size.width;
CGFloat height = imgTest.layer.frame.size.height;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 30, 0);
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width, height);
CGPathAddLineToPoint(path, nil, 0, height);
CGPathAddLineToPoint(path, nil, 30, 0);
CGPathCloseSubpath(path); mask.path = path;
CGPathRelease(path);
imgTest.layer.mask = mask;

那么如何画出第一张图像形状呢?

1 个答案:

答案 0 :(得分:1)

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = imgTest.layer.bounds;
CGFloat width = imgTest.layer.frame.size.width;
CGFloat height = imgTest.layer.frame.size.height;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, 0);
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width-30, height);
CGPathAddLineToPoint(path, nil, 0, height);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path); mask.path = path;
CGPathRelease(path);
imgTest.layer.mask = mask;