除非绝对必要,否则我不想重新发明轮子,所以我想看看如何使用 Core Graphics 绘制这样的箭头:
有人知道我如何开始使用它吗? 这是我目前所拥有的,但它绘制了一个正三角形而不是括号形状:
CGPoint origin = CGPointMake(100, 20);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:origin];
[path addLineToPoint:CGPointMake(origin.x-10, origin.y-20)];
[path addLineToPoint:CGPointMake(origin.x-10, origin.y+20)];
[[UIColor redColor] set];
[path fill];
最佳答案
如果按照@NSGod 的建议使用笔画绘制,则需要移动到上端或下端,然后连线到箭头,然后连线到下端或上端。如果你想要一个像你画的那样的 45 度角,你添加或减去坐标的量应该相等。
您还需要设置路径的线宽。这需要与尺寸成比例。
CGPoint origin = CGPointMake(100, 20);
UIBezierPath *path = [UIBezierPath bezierPath];
// Upper tip
[path moveToPoint:CGPointMake(origin.x+20, origin.y-20)];
// Arrow head
[path addLineToPoint:CGPointMake(origin.x, origin.y)];
// Lower tip
[path addLineToPoint:CGPointMake(origin.x+20, origin.y+20)];
[[UIColor redColor] set];
// The line thickness needs to be proportional to the distance from the arrow head to the tips. Making it half seems about right.
[path setLineWidth:10];
[path stroke];
结果是这样的形状。
如果您想要填充,而不是描边 - 如果您想要勾勒出箭头的轮廓,这可能是必要的 - 您将需要绘制出 6 个点,然后关闭路径。
关于iphone - 在 Core Graphics 中绘制子弹箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10326053/