大佬教程收集整理的这篇文章主要介绍了swift – 带圆角的虚线,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 } }
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 – 带圆角的虚线所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。