我有自己的自定义视图,就像进度条一样。这是它的代码:
class ProgressBar: UIView {
var bottomProgressBar = CAShapeLayer()
var topProgressBar = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
bottomProgressBar.strokeColor = UIColor.white.cgColor
bottomProgressBar.opacity = 0.1
bottomProgressBar.lineWidth = self.frame.height
bottomProgressBar.strokeStart = 0
bottomProgressBar.strokeEnd = 1
self.layer.addSublayer(bottomProgressBar)
topProgressBar.strokeColor = UIColor.white.cgColor
topProgressBar.lineWidth = self.frame.height
topProgressBar.strokeStart = 0
topProgressBar.strokeEnd = 1
self.layer.addSublayer(topProgressBar)
animate(toValue: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
bottomProgressBar.path = path.cgPath
topProgressBar.path = path.cgPath
}
}
现在,当我使用情节提要板添加此控件时,通过UIView
在控制器内部添加控件并将其设置为Custom Class
,ProgressBar
我可以在运行应用程序时看到它(它应为白色,alpha为0.1)。
但是,当我尝试以编程方式添加时,它根本不可见。仅当我明确设置背景色时:
myProgressBar.backgroudColor = .red
这不是我想要的。我希望它具有白色和作为初始值的alpha 0.1。
这是我以编程方式添加它的代码:
var progressBar = ProgressBar()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(progressBar)
progressBar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 1).isActive = true
}
我似乎不明白使用情节提要添加它和以编程方式添加它之间有什么区别。
我看到约束是正确的,并且添加了视图,但是为什么不显示默认颜色?
有谁知道在这种情况下我的问题是什么?
编辑:
为该animate
方法添加了代码:
func animate(toValue: Double) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 1.0
animation.fromValue = 0
animation.toValue = toValue
topProgressBar.strokeEnd = CGFloat(toValue)
topProgressBar.animation(forKey: "animate")
}
问题始于此var progressBar = ProgressBar()
。这将调用init方法并被configure()
调用。此时ProgressBar
框架的宽度和高度为zero
。
在您的configure()
方法中,您正在设置线宽。
bottomProgressBar.lineWidth = self.frame.height // 0
topProgressBar.lineWidth = self.frame.height // 0
因此,将线宽设置为zero
,这样就不会显示它。
要解决此问题,您必须CAShapeLayer
在layoutSubviews()
方法中更新线宽。
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
bottomProgressBar.lineWidth = self.frame.height // Update lineWidth
topProgressBar.lineWidth = self.frame.height // Update lineWidth
bottomProgressBar.path = path.cgPath
topProgressBar.path = path.cgPath
}
为什么适用于情节提要?因为在configure
调用之前已经设置好帧。您可以打印帧大小configure
并查看两种情况下的差异。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句