ios - 如何更改此 UIBezierPath 的箭头方向
<p><p>我有以下代码,它绘制了一个圆形矩形,顶部有一个小插入符号箭头。我希望能够创建一个方法,根据我指定的内容在顶部、左侧、右侧或底部绘制箭头(我可能会传入一个枚举 typedef)。</p>
<p>这是我的代码现在生成的内容:</p>
<p> <img src="/image/ERiQA.png" alt="enter image description here"/> </p>
<p>谁能帮我使这段代码更加动态/灵活,这样我就可以完成这项工作?</p>
<pre><code> 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);
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我可以给你点开始。
首先,我将矩形和箭头分开:</p>
<p>绘制简单的矩形:</p>
<pre><code>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 = ;
;
;
</code></pre>
<p>添加为 subviewf.e:</p>
<pre><code>- (void)viewDidLoad {
;
BoxView *box = [ initWithFrame:CGRectMake(100, 100, 200, 80)];
box.backgroundColor = ; // I specially set red color to see it's full surface...
;
}
</code></pre>
<p> <img src="/image/PmCpX.png" alt="enter image description here"/> </p>
<p>然后在矩形中心绘制简单的箭头</p>
<pre><code>const CGFloat kArrowWidth = 10;
// Draw Bezier arrow on top consists of 3 points
UIBezierPath* bezierPathTop = UIBezierPath.bezierPath;
;
;
;
;
;
</code></pre>
<p> <img src="/image/F9XQs.png" alt="enter image description here"/> </p>
<p>最后是相同的箭头,但点位置不同</p>
<pre><code>UIBezierPath* bezierPathLeft = UIBezierPath.bezierPath;
;
;
;
;
;
</code></pre>
<p> <img src="/image/AppRx.png" alt="enter image description here"/> </p>
<p>您只需要根据我们说的箭头方向枚举确定只绘制一个箭头。我希望您可以通过此示例走得更远</p></p>
<p style="font-size: 20px;">关于ios - 如何更改此 UIBezierPath 的箭头方向,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26982670/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26982670/
</a>
</p>
页:
[1]