swift – 带圆角的虚线

swift – 带圆角的虚线,第1张

概述参见英文答案 > CoreGraphics line appears to change size?                                    2个 我试图在这样的视图周围绘制带圆角的虚线: class DashedLineView: UIView { override func draw(_ rect: CGRect) { let path 参见英文答案 > CoreGraphics line appears to change size?2个
我试图在这样的视图周围绘制带圆角的虚线:
class DashedlineVIEw: UIVIEw {    overrIDe func draw(_ rect: CGRect) {        let path = UIBezIErPath(roundedRect: rect,cornerRadius: 8)        UIcolor.clear.setFill()        path.fill()        UIcolor.red.setstroke()        path.linewidth = 3        let dashPattern : [CGfloat] = [3,3]        path.setlineDash(dashPattern,count: 2,phase: 0)        path.stroke()    }}

结果是:

你可以看到角落有问题,任何想法如何修复它?

更新:
使用@Jon Rose answer DashedlineVIEw现在看起来像这样:

class DashedlineVIEw: UIVIEw {    private let borderLayer = CAShapeLayer()    overrIDe func awakeFromNib() {        super.awakeFromNib()        borderLayer.strokecolor = UIcolor.red.cgcolor        borderLayer.lineDashPattern = [3,3]        borderLayer.backgroundcolor = UIcolor.clear.cgcolor        borderLayer.fillcolor = UIcolor.clear.cgcolor        layer.addSublayer(borderLayer)    }    overrIDe func draw(_ rect: CGRect) {        borderLayer.path = UIBezIErPath(roundedRect: rect,cornerRadius: 8).cgPath    }}
我在使用CAShapeLayer方面有很好的经验.例如:
let rect = CGRect.init(origin: CGPoint.init(x: 20,y: 100),size: CGSize.init(wIDth: 200,height: 100))let layer = CAShapeLayer.init()let path = UIBezIErPath(roundedRect: rect,cornerRadius: 8)layer.path = path.cgPath;layer.strokecolor = UIcolor.red.cgcolor;layer.lineDashPattern = [3,3];layer.backgroundcolor = UIcolor.clear.cgcolor;layer.fillcolor = UIcolor.clear.cgcolor;self.vIEw.layer.addSublayer(layer);

作为奖励,几乎所有CAShapeLayer的属性都是可动画的,包括lineDashPhase,这意味着你可以让它看起来像破折号在盒子周围移动.

总结

以上是内存溢出为你收集整理的swift – 带圆角的虚线全部内容,希望文章能够帮你解决swift – 带圆角的虚线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存