在Core Graphics中绘制子弹箭头

Sno*_*man 4 iphone core-graphics objective-c ios

除非绝对必要,否则我不想重新发明轮子,所以我想看看如何使用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];
Run Code Online (Sandbox Code Playgroud)

Don*_*mer 9

如果您使用@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];
Run Code Online (Sandbox Code Playgroud)

结果是这样的形状.

上面的代码绘制的箭头

如果你想要填充,而不是中风 - 如果你想要勾画你的箭头可能是必要的 - 你需要绘制出6个点,然后关闭路径.

  • 只是想补充一点,在iOS 5及以上版本中你可以使用[CGPathCreateCopyByStrokingPath](https://developer.apple.com/library/ios/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/func/CGPathCreateCopyByStrokingPath)而不是自己绘制角点以实现可以填充而不是抚摸的路径. (2认同)