【问题标题】:CAShapeLayer for draw triangle with rectangleCAShapeLayer 用于绘制带矩形的三角形
【发布时间】:2016-01-06 10:29:32
【问题描述】:

我想像这张图片一样用矩形绘制三角形。

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

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;

那么我怎样才能像第一个图像形状一样绘制呢?

【问题讨论】:

  • 把你的电脑倒过来。但说真的 - 我认为如果您只是尝试理解您拥有的代码,您应该能够轻松更改它。
  • @rghome 是的。我对 CAShapeLayer 做了一些研究,但我不明白如何设置 CGPathMoveToPoint 和 CGPathAddLineToPoint。看到下面的答案我就可以理解了。

标签: ios objective-c iphone cashapelayer


【解决方案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;

【讨论】:

  • 是的。这就是答案。非常感谢。
  • @Hemal。欢迎。请掩盖答案:)
猜你喜欢
  • 1970-01-01
  • 2014-12-10
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多