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

CAShapeLayer for draw triangle with rectangle

我想像这个图片一样用矩形画三角形。

但是我可以使用这段代码画出这样的形状。

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;

那么我怎样才能画出第一个图像的形状呢?

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;