如何更改此UIBezierPath的箭头方向

时间:2014-11-17 21:51:50

标签: ios objective-c core-graphics quartz-graphics uibezierpath

我有以下代码,它绘制一个圆角矩形,顶部有一个小插入箭头。我希望能够创建一个方法,根据我指定的内容,在顶部,左侧,右侧或底部绘制箭头(我可能会传入枚举类型定义)。

以下是我的代码现在生成的内容:

enter image description here

任何人都可以帮助我使这段代码更具动态性/灵活性,这样我才能做到这一点吗?

        CGContextBeginPath(context);
        CGContextMoveToPoint(context, kBubbleBorderRadius + 0.5f, kCaretHeight + 0.5f);
        CGContextAddLineToPoint(context, round(currentFrame.size.width / 2.0f - (kCaretHeight * 2.0) / 2.0f) + 0.5f, kCaretHeight + 0.5f);
        CGContextAddLineToPoint(context, round(currentFrame.size.width / 2.0f) + 0.5f, 0.5f);
        CGContextAddLineToPoint(context, round(currentFrame.size.width / 2.0f + (kCaretHeight * 2.0) / 2.0f) + 0.5f, kCaretHeight + 0.5f);
        CGContextAddArcToPoint(context, currentFrame.size.width - 0.5f, kCaretHeight + 0.5f, currentFrame.size.width- 0.5f, currentFrame.size.height - 0.5f, kBubbleBorderRadius);
        CGContextAddArcToPoint(context, currentFrame.size.width - 0.5f, currentFrame.size.height - 0.5f, round(currentFrame.size.width / 2.0f + (kCaretHeight * 2.0) / 2.0f) + 0.5f, currentFrame.size.height - 0.5f, kBubbleBorderRadius);
        CGContextAddArcToPoint(context, 0.5f, currentFrame.size.height - 0.5f, 0.5f, kCaretHeight + 0.5f, kBubbleBorderRadius);
        CGContextAddArcToPoint(context, 0.5f, kCaretHeight + 0.5f, currentFrame.size.width - 0.5f, kCaretHeight + 0.5f, kBubbleBorderRadius);
        CGContextClosePath(context);
        CGContextDrawPath(context, kCGPathFill);

1 个答案:

答案 0 :(得分:1)

我可以给你点开始。 首先,我将矩形和箭头分开:

绘制简单的矩形:

const CGFloat kRectangleOffset = 5; // This is offset to have some space for arrow

// Draw simple rectangle in DrawRect method
    CGRect frame = CGRectMake(0+kRectangleOffset,
                              0+kRectangleOffset,
                              rect.size.width-kRectangleOffset*2,
                              rect.size.height-kRectangleOffset*2);

    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: frame];
    [UIColor.grayColor setFill];
    [rectanglePath fill];

添加为子视图f.e:

- (void)viewDidLoad {
    [super viewDidLoad];

    BoxView *box = [[BoxView alloc] initWithFrame:CGRectMake(100, 100, 200, 80)];
    box.backgroundColor = [UIColor redColor]; // I specially set red color to see it's full surface...
    [self.view addSubview:box];
}

enter image description here

然后在矩形中心绘制简单的箭头

const CGFloat kArrowWidth = 10;

  // Draw Bezier arrow on top consists of 3 points 
    UIBezierPath* bezierPathTop = UIBezierPath.bezierPath;
    [bezierPathTop moveToPoint: CGPointMake(rect.size.width/2-kArrowWidth/2, kRectangleOffset)];
    [bezierPathTop addLineToPoint: CGPointMake(rect.size.width/2+kArrowWidth/2, kRectangleOffset)];
    [bezierPathTop addLineToPoint: CGPointMake(rect.size.width/2, 0)];
    [UIColor.blueColor setFill];
    [bezierPathTop fill];

enter image description here

最后是相同的箭头,但位置不同

UIBezierPath* bezierPathLeft = UIBezierPath.bezierPath;
[bezierPathLeft moveToPoint: CGPointMake(kRectangleOffset, rect.size.height/2-kArrowWidth/2)];
[bezierPathLeft addLineToPoint: CGPointMake(kRectangleOffset, rect.size.height/2+kArrowWidth/2)];
[bezierPathLeft addLineToPoint: CGPointMake(0, rect.size.height/2)];
[UIColor.blueColor setFill];
[bezierPathLeft fill];

enter image description here

根据让我们说箭头方向枚举,您需要只绘制一个箭头。我希望你能进一步了解这个样本