菜鸟教程小白 发表于 2022-12-13 14:16:44

ios - 如何关闭三角形路径?


                                            <p><p>我画了这个形状:</p>

<p> <a href="/image/CxZFl.png" rel="noreferrer noopener nofollow"><img src="/image/CxZFl.png" alt="enter image description here"/></a> </p>

<p>这是我的代码:</p>

<pre><code>/* build path */
let bottomBezierPath = UIBezierPath()
//first thing draw bottom line
bottomBezierPath.moveToPoint(CGPointMake(0, TRIANGLE_EDGE))
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width,TRIANGLE_EDGE))

//now draw triangle
bottomBezierPath.moveToPoint(CGPointMake(bottomShapeLayer.frame.size.width,TRIANGLE_EDGE))
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width,0))
bottomBezierPath.moveToPoint(CGPointMake(bottomShapeLayer.frame.size.width,0))
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width - TRIANGLE_EDGE ,TRIANGLE_EDGE))

bottomShapeLayer.path = bottomBezierPath.CGPath
bottomShapeLayer.fillColor = UIColor.redColor().CGColor
bottomShapeLayer.strokeColor = UIColor.redColor().CGColor
</code></pre>

<p>但我需要用红色填充三角形,<code>bottomShapeLayer.fillColor</code>应该这样做,对吧?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><h2>您正在为三角形创建 2 个单独的线段。</h2>

<p><strong>每次使用 <code>moveToPoint()</code> 时,都会开始一个新的线段。</strong>因此,当 <code>CAShapeLayer</code> 来填充三角形,无法填充它,因为它“不完整”。</p>

<p>解决这个问题<em>真的</em>很简单,您只需通过删除 <code>moveToPoint()</code> 调用之一为三角形创建连续路径:</p>

<pre><code>let bottomBezierPath = UIBezierPath()
//first thing draw bottom line
bottomBezierPath.moveToPoint(CGPointMake(0, TRIANGLE_EDGE))
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width,TRIANGLE_EDGE))

//now draw triangle
//bottomBezierPath.moveToPoint(CGPointMake(bottomShapeLayer.frame.size.width,TRIANGLE_EDGE)) // this line is redundant btw, as you&#39;re moving to the same point. I&#39;ve left it in incase you want to start your triangle somewhere else.
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width,0))
bottomBezierPath.addLineToPoint(CGPointMake(bottomShapeLayer.frame.size.width - TRIANGLE_EDGE ,TRIANGLE_EDGE))
</code></pre>

<p><strong>还值得注意的是,在三角形的<em>开始</em>调用 <code>moveToPoint()</code> 也是多余的,因为它正在移动到同一点。</强></p>

<p>现在你有一个完整的三角形,它应该被正确填充。</p>

<p> <a href="/image/06myT.png" rel="noreferrer noopener nofollow"><img src="/image/06myT.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何关闭三角形路径?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35343021/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35343021/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何关闭三角形路径?