gpt4 book ai didi

ios - 使用 IBDesignable 的水平虚线

转载 作者:行者123 更新时间:2023-11-28 05:47:21 26 4
gpt4 key购买 nike

所以我遇到了this问题,我想实现这一点,使用 @IBDesignable 以相同的方法绘制一条水平线。

我试过在类里面玩耍,但没有结果。

@IBDesignable class DottedVertical: UIView {

@IBInspectable var dotColor: UIColor = UIColor.etc
@IBInspectable var lowerHalfOnly: Bool = false

override func draw(_ rect: CGRect) {

// say you want 8 dots, with perfect fenceposting:
let totalCount = 8 + 8 - 1
let fullHeight = bounds.size.height
let width = bounds.size.width
let itemLength = fullHeight / CGFloat(totalCount)

let path = UIBezierPath()

let beginFromTop = CGFloat(0.0)
let top = CGPoint(x: width/2, y: beginFromTop)
let bottom = CGPoint(x: width/2, y: fullHeight)

path.move(to: top)
path.addLine(to: bottom)

path.lineWidth = width

let dashes: [CGFloat] = [itemLength, itemLength]
path.setLineDash(dashes, count: dashes.count, phase: 0)

// for ROUNDED dots, simply change to....
//let dashes: [CGFloat] = [0.0, itemLength * 2.0]
//path.lineCapStyle = CGLineCap.round

dotColor.setStroke()
path.stroke()
}
}

最佳答案

你可以实现如下,

@IBDesignable class DottedHorizontal: UIView {

@IBInspectable var dotColor: UIColor = UIColor.red
@IBInspectable var lowerHalfOnly: Bool = false

override func draw(_ rect: CGRect) {

let fullHeight = bounds.size.height
let width = bounds.size.width

let path = UIBezierPath()

path.move(to: CGPoint(x: 0, y: fullHeight/2))
path.addLine(to: CGPoint(x: width, y: fullHeight/2))

path.lineWidth = 5

let dashes: [CGFloat] = [4, 2]
path.setLineDash(dashes, count: dashes.count, phase: 0)

dotColor.setStroke()
path.stroke()
}
}

enter image description here

关于ios - 使用 IBDesignable 的水平虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178402/

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