菜鸟教程小白 发表于 2022-12-12 14:58:17

ios - 如何在 View 的框架上添加虚线边框而不是边界 [Swift]


                                            <p><p>我正在尝试在 View 上添加一个虚线边框,该边框将始终位于 View 的框架上,而不是在它的 bound 上。 </p>

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

<pre><code>func addDashedBorder() {

    let color = UIColor.red.cgColor
    shapeLayer = CAShapeLayer()
    let frameSize = self.bounds.size
    let shapeRect = CGRect(x:0 , y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.frame = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPattern =
    shapeLayer.path = UIBezierPath(roundedRect: self.frame, cornerRadius: 5).cgPath

    self.layer.addSublayer(shapeLayer)
}
</code></pre>

<p><strong>结果</strong><br/>
<a href="/image/t5yS3.png" rel="noreferrer noopener nofollow"><img src="/image/t5yS3.png" alt="enter image description here"/></a> </p>

<p>如果我们旋转 View ,我不想旋转 View 上的边框。
它应该表明它在 View 中占据了多少空间。</p>

<p><strong>期待</strong></p>

<p> <a href="/image/cXfNi.png" rel="noreferrer noopener nofollow"><img src="/image/cXfNi.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要采取两个 View ,即内部 View 和外部 View 。内 View (旋转 View )应该在外 View 的 subview 中。两个 View 的框架必须相同。使用以下代码行 - </p>

<pre><code>@IBOutlet weak var viewOuter: UIView! //static outer view
@IBOutlet weak var viewInner: UIView! // View that will be use for rotation

func addDashedBorder() {

    let color = UIColor.red.cgColor
    let shapeLayer = CAShapeLayer()
    let frameSize = viewInner.bounds.size
    let shapeRect = CGRect(x:0 , y: 0, width: frameSize.width, height: frameSize.height)
    shapeLayer.frame = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.lineDashPattern =
    shapeLayer.path = UIBezierPath(roundedRect: viewInner.frame, cornerRadius: 5).cgPath
    self.viewOuter.layer.addSublayer(shapeLayer)

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 View 的框架上添加虚线边框而不是边界 ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52714622/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52714622/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 View 的框架上添加虚线边框而不是边界 [Swift]