【发布时间】:2018-06-01 12:15:33
【问题描述】:
我有我的自定义视图,我将其用作加载栏。它有一个不透明度为 0.1 的底层和一个不透明度为 1 的顶层。
设置如下:
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
animation.delegate = self
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(fromValue: 0, 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.lineWidth = self.frame.height // Update lineWidth
topProgressBar.lineWidth = self.frame.height // Update lineWidth
bottomProgressBar.path = path.cgPath
topProgressBar.path = path.cgPath
self.clipsToBounds = true
self.layer.cornerRadius = 4
}
现在我也想应用圆角半径,所以我在layoutSubviews的底部添加了代码:
self.clipsToBounds = true
self.layer.cornerRadius = 4
这仅将角半径应用于左上角和右上部分,就是这样。我不明白为什么。
我还尝试将其设置在configure 方法中,用于bottomProgressBar 和topProgressBar 层,但根本没有用。
有人能指出这里的问题吗?
编辑:
这是完整的课程代码:
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: 1)
}
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
}
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")
}
}
编辑 2:
添加了更多关于正在发生的事情的屏幕截图:
这没有应用圆角半径 - 简单矩形
这就是我应用圆角半径时发生的情况 - 下半部分被切掉(矩形显然是半切)
这就是我想要的 - 具有四个角的圆角半径的简单矩形:
【问题讨论】:
-
你设置没有框架的图层,在init中做动画,这是一个混乱的视图
-
init 中的动画只是为了有一个默认值。仍然没有解释圆角半径问题。
-
发布完整课程 - 您显示的代码不能按原样使用,因此很难尝试帮助找出问题所在。
-
@DonMag:完成。我添加了 ProgressBar 类的代码。
-
@Kobe - 看起来您只是将一条线叠加在另一条线上...您能展示一张您想要它的外观的图片吗?