gpt4 book ai didi

ios - 用 CAShapeLayer 画一条线

转载 作者:可可西里 更新时间:2023-11-01 00:57:32 26 4
gpt4 key购买 nike

我想显示这样一行:

enter image description here

这是我的代码:

import UIKit

@IBDesignable
class ProgressView: UIView {

let π = CGFloat(M_PI)
var levelProgress: CGFloat = 0.0 {
didSet {
self.fgLayer.strokeEnd = levelProgress
}
}

let bgLayer = CAShapeLayer()
let fgLayer = CAShapeLayer()

@IBInspectable var bgColor: UIColor = UIColor.black.withAlphaComponent(0.3) {
didSet {
configure()
}
}

@IBInspectable var fgColor: UIColor = UIColor.white {
didSet {
configure()
}
}

override func awakeFromNib() {
super.awakeFromNib()

setup()
configure()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()

setup()
configure()
}

func setup() {
bgLayer.lineWidth = 4.0
bgLayer.fillColor = nil
bgLayer.strokeEnd = 1
layer.addSublayer(bgLayer)
fgLayer.lineWidth = 4.0
fgLayer.fillColor = nil
fgLayer.strokeEnd = 0.0
layer.addSublayer(fgLayer)
}

func configure() {
bgLayer.strokeColor = bgColor.cgColor
fgLayer.strokeColor = fgColor.cgColor
}

override func layoutSubviews() {
super.layoutSubviews()

setupShapeLayer(shapeLayer: bgLayer)
setupShapeLayer(shapeLayer: fgLayer)
}

private func setupShapeLayer(shapeLayer: CAShapeLayer) {
shapeLayer.frame = self.bounds
let linePath = UIBezierPath()
linePath.move(to: self.frame.origin)
linePath.addLine(to: CGPoint(x: self.frame.origin.x + self.frame.width, y: self.frame.origin.y))
shapeLayer.path = linePath.cgPath
}

}

但是运行之后什么也没有显示

最佳答案

问题是 UIBezierPath 的 startPoint 和 endPoint 设置错误。应该是这样的:

private func setupShapeLayer(shapeLayer: CAShapeLayer) {
shapeLayer.frame = self.bounds
let linePath = UIBezierPath()
linePath.move(to: CGPoint(x: 0, y: 0))
linePath.addLine(to: CGPoint(x: self.frame.width, y: 0))
shapeLayer.path = linePath.cgPath
}

关于ios - 用 CAShapeLayer 画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42199944/

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