gpt4 book ai didi

ios - 如何为 UIView 的任何特定边缘添加虚线边框?

转载 作者:行者123 更新时间:2023-11-28 15:19:59 27 4
gpt4 key购买 nike

如何只在 UIView 的某边添加虚线边框?引用Dashed line border around UIView链接,但在给出 View 所有边缘的路径时我有点困惑。好吧,这是我在 View 的特定一侧添加直线边框的代码:

 I want this as my output screen

func addRightBorder(with color: UIColor, andWidth borderWidth: CGFloat,view:UIView) {

let border = UIView()
border.backgroundColor = color
border.autoresizingMask = [.flexibleHeight, .flexibleLeftMargin]
border.frame = CGRect(x: view.frame.size.width - borderWidth, y: 0, width: borderWidth, height: view.frame.size.height)

//border.addDashedLine(color: UIColor.red)
border.addDashedLine(color: UIColor.red)
view.addSubview(border)
}*

除了虚线/点线,我怎样才能达到同样的效果?

最佳答案

在我的一个应用程序中,我也需要类似的东西,因此我创建了 UIView Extension。

看看我的代码片段,

extension UIView {
func removeDashedLine() {
_ = layer.sublayers?.filter({ $0.name == "DashedTopLine" }).map({ $0.removeFromSuperlayer() })
}

func addDashedLine(_ path: UIBezierPath ,pattern : [NSNumber] = [1, 5], color: UIColor = UIColor.red) {

self.backgroundColor = UIColor.clear

let shapeLayer: CAShapeLayer = CAShapeLayer()
let frameSize = self.frame.size
let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

shapeLayer.name = "DashedTopLine"
shapeLayer.frame = shapeRect
//shapeLayer.position = CGPoint(x: frameSize.width / 2, y: frameSize.height / 2)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = color.cgColor
shapeLayer.lineWidth = 1
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineDashPattern = pattern

shapeLayer.path = nil
shapeLayer.path = path.cgPath
self.layer.insertSublayer(shapeLayer, at: 0)

}
}

它可能无法完全满足您的要求,但应该可以帮助您

关于ios - 如何为 UIView 的任何特定边缘添加虚线边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46174348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com