Iphone 在核心图形中绘制项目符号箭头

Iphone 在核心图形中绘制项目符号箭头,iphone,objective-c,ios,core-graphics,Iphone,Objective C,Ios,Core Graphics,除非绝对必要,否则我不想重新发明轮子,所以我想看看如何用核心图形绘制箭头: 有人知道我该怎么开始吗? 这是我到目前为止所做的,但它绘制的是正三角形,而不是括号形状: CGPoint origin = CGPointMake(100, 20); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:origin]; [path addLineToPoint:CGPointMake(ori

除非绝对必要,否则我不想重新发明轮子,所以我想看看如何用核心图形绘制箭头:

有人知道我该怎么开始吗? 这是我到目前为止所做的,但它绘制的是正三角形,而不是括号形状:

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

我不是在电脑前写代码,但基本上你要创建一个CGMutablePath,添加然后执行

CGContextStrokePath(context);

您可以设置所需效果的笔划宽度和线帽

问题的一个潜在部分是您使用的是
填充
而不是
笔划

通过将
[path fill]
更改为
[path stroke]
,您将得到以下结果:

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];
[path stroke];

这就提出了一个问题,即
原点的意图是什么。它将真正转化为箭头点,而不是传统意义上的原点使用方式(将意味着绘制对象的左上角)

如果要保留源代码的含义,需要将代码修改为以下内容:

CGPoint origin = CGPointMake(100, 20);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(origin.x - 20, origin.y - 20)];
[path addLineToPoint:origin];
[path addLineToPoint:CGPointMake(origin.x - 20, origin.y + 20)];
path.lineWidth = 4.0;
[[UIColor redColor] set];
[path stroke];
或者,如果您想使用传统意义上的
来源
,您可以使用:

CGPoint origin = CGPointMake(100, 20);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:origin];
[path addLineToPoint:CGPointMake(origin.x + 20, origin.y + 20)];
[path addLineToPoint:CGPointMake(origin.x, origin.y + 20 * 2)];
path.lineWidth = 4.0;
[[UIColor redColor] set];
[path stroke];
这将产生以下结果:

CGPoint origin = CGPointMake(100, 20);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(origin.x - 20, origin.y - 20)];
[path addLineToPoint:origin];
[path addLineToPoint:CGPointMake(origin.x - 20, origin.y + 20)];
path.lineWidth = 4.0;
[[UIColor redColor] set];
[path stroke];

如果您使用@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个点,然后关闭路径。

这里有一个Swift 3.0兼容类,源代码的学分取自上面@Dondragmer的答案

您可以更改以下选项:

  • 线宽:CGFloat
  • 线条颜色:UIColor
  • 方向:箭头方向(枚举为.up、.down、.left、.right)

我知道…这是我需要帮助的几何图形这是Paintcodeapp可以派上用场的地方。天哪,太棒了!我不知道有这样的东西存在。我可能会把它画成六点形状,而不是三点路径。有没有其他像Paintcodeapp这样的酷应用可以帮助绘画或动画?只是想补充一下,在iOS 5和更高版本中,你可以使用它来代替自己绘制角点,从而获得一条可以填充的路径,而不是笔划。我也在尝试这样做。我必须将此添加到viewDidLoad吗?很高兴我向下滚动了。很不错的。
let arrow = Arrow(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
arrow.lineWidth = 20
arrow.lineColor = UIColor.blue
arrow.direction = .up