如何将箭头附加到 UIBezierPath

How to append arrow head to UIBezierPath

我需要帮助将箭头添加到 UIBezierPath。 我使用 UIBezierPath 创建了多条线,它们位于不同的方向。 现在,我想在线的终点创建箭头。

我尝试使用 NSAttributedString 将 UIImage 添加到 UILable 到使用 NSTextAttachment 的 UILable。

但是,这里的问题是箭头总是向下。我想沿线对齐箭头。

这是我的代码:

    UIBezierPath *ShotPath = [UIBezierPath bezierPath];
    NSValue *PointValue = [[[PathArray objectAtIndex:i] valueForKey:@"ShotPath"] objectAtIndex:k];
    CGPoint FirstPoint; = [PointValue CGPointValue];
    [ShotPath moveToPoint:FirstPoint];

    CAShapeLayer *line = [CAShapeLayer layer];
    line.lineWidth = 4.0;
    line.path=ShotPath.CGPath;
    line.fillColor = [UIColor clearColor].CGColor;
    line.strokeColor = [UIColor colorWithRed:231/255.0 green:83/255.0 blue:73/255.0 alpha:1].CGColor;
    [[BaseView layer] addSublayer:line];
    NSValue *PointValue = [[[PathArray objectAtIndex:i] valueForKey:@"ShotPath"] objectAtIndex:[[[PathArray objectAtIndex:i] valueForKey:@"ShotPath"] count]-1];
    CGPoint OtherPoint = [PointValue CGPointValue];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(OtherPoint.x-6, OtherPoint.y-8, 12, 12)];
    lbl.backgroundColor =[UIColor clearColor];
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"DropDown_Icon"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
    lbl.attributedText = attachmentString;
    [lbl setTextColor:[UIColor blackColor]];
    lbl.font =[UIFont boldSystemFontOfSize:22.0];
    [BaseView addSubview:lbl];

这是我当前的输出:

参考本回答:

解决了我的问题: 答案:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint point1 = CGPointMake(100, 100);
    CGPoint point2 = CGPointMake(300, 300);

    [path moveToPoint:point1];
    [path addQuadCurveToPoint:point2 controlPoint:point1];

    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    shape.lineWidth = 10;
    shape.strokeColor = [UIColor blueColor].CGColor;
    shape.fillColor = [UIColor clearColor].CGColor;
    shape.frame = self.view.bounds;
    [self.view.layer addSublayer:shape];

    CGFloat angle = atan2f(point2.y - point1.y, point2.x - point1.x);

    CGFloat distance = 15.0;
    path = [UIBezierPath bezierPath];
    [path moveToPoint:point2];
    [path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle + M_PI_2 distance:distance]]; // to the right
    [path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle          distance:distance]]; // straight ahead
    [path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle - M_PI_2 distance:distance]]; // to the left
    [path closePath];

    shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    shape.lineWidth = 2;
    shape.strokeColor = [UIColor redColor].CGColor;
    shape.fillColor = [UIColor redColor].CGColor;
    shape.frame = self.view.bounds;
    [self.view.layer addSublayer:shape];
}
- (CGPoint)calculatePointFromPoint:(CGPoint)point angle:(CGFloat)angle distance:(CGFloat)distance {
    return CGPointMake(point.x + cosf(angle) * distance, point.y + sinf(angle) * distance);
}