iphone - 在 Core Graphics 中绘制子弹箭头

标签 iphone objective-c ios core-graphics

除非绝对必要,否则我不想重新发明轮子,所以我想看看如何使用 Core Graphics 绘制这样的箭头:

enter image description here

有人知道我如何开始使用它吗? 这是我目前所拥有的,但它绘制了一个正三角形而不是括号形状:

    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];

结果是这样的形状。

Arrow drawn by code above

如果您想要填充,而不是描边 - 如果您想要勾勒出箭头的轮廓,这可能是必要的 - 您将需要绘制出 6 个点,然后关闭路径。

关于iphone - 在 Core Graphics 中绘制子弹箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10326053/

相关文章:

ios - 使用 DatePicker 展开和折叠 UITableViewCells

Objective-C @interface/指针澄清

android - 在移动设备上的所有用户之间共享 Google map

iphone - 对于某些纹理尺寸,glFramebufferTexture2D 在 iPhone 上失败

ios - Swift:UIWebView 加载旧内容

iphone - 为什么 UIButtons 无法检测到 UIActionSheet 上的 UIView 中的触摸?

iphone - 如何正确定义结构

iphone - 将 url 参数拆分并转换为字符串值的最佳方法

objective-c - Linea pro 设备检测

ios - 设置UILabel的frame时,是否可以在代码中将其设置到superview的垂直和水平中心?