Ios 如何更改此UIBezierPath的箭头方向
我有下面的代码,它绘制了一个圆形的矩形,顶部有一个小的插入符号箭头。我希望能够创建一个方法,根据我指定的内容在顶部、左侧、右侧或底部绘制箭头(我可能会传入一个枚举typedef) 下面是我的代码现在生成的内容: 有谁能帮我使这段代码更加动态/灵活,这样我就可以完成这项任务Ios 如何更改此UIBezierPath的箭头方向,ios,objective-c,core-graphics,quartz-graphics,uibezierpath,Ios,Objective C,Core Graphics,Quartz Graphics,Uibezierpath,我有下面的代码,它绘制了一个圆形的矩形,顶部有一个小的插入符号箭头。我希望能够创建一个方法,根据我指定的内容在顶部、左侧、右侧或底部绘制箭头(我可能会传入一个枚举typedef) 下面是我的代码现在生成的内容: 有谁能帮我使这段代码更加动态/灵活,这样我就可以完成这项任务 CGContextBeginPath(context); CGContextMoveToPoint(context, kBubbleBorderRadius + 0.5f, kCaretHei
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);
我可以给你一个起点。 首先,我将矩形和箭头分开: 绘制简单矩形:
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];
}
然后在矩形的中心绘制简单的箭头
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];
最后是相同的箭头,但有不同的点位置
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];
您需要根据箭头方向枚举确定只绘制一个箭头。我希望您能进一步了解此示例您能提供所需的图形表示吗…@Injectios为问题添加了当前代码生成的图像,谢谢。如果您将三角形与主体分开绘制,会更容易。@thelaws,我同意。在这种情况下,我如何区分方法?