ios - 如何将箭头附加到 UIBezierPath

标签 ios objective-c uibezierpath quartz-core

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

我尝试使用 NSAttributedString 将 UIImage 添加到 UILable,使用 NSTextAttachment 将 UIImage 添加到 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];

这是我当前的输出:

enter image description here

最佳答案

引用这个答案:

append arrowhead to UIBezierPath

解决了我的问题: 答:

enter image description here

- (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);
}

关于ios - 如何将箭头附加到 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51279428/

相关文章:

android - Phonegap 的 IDE 是什么? Eclipse 够用吗?

iOS 12 企业应用程序在启动时崩溃

ios - UITableView 拖动结束

ios - 获取 uitextview 中插入符号所在行的行号

ios - Xcode 7 不显示添加的图像

objective-c - MGSplitViewController 更改详细 View Controller

ios - 如何获取 UIImage 中形状的 UIBezierPath 或以特定形状裁剪 UIImage

ios - (NSDictionary<NSString *,id> * __nonnull) 是什么意思?

Swift UIBezierPath 不同坐标起点

ios - 用 UIBezierPath 画一条线