【发布时间】:2017-11-09 17:17:03
【问题描述】:
好的,事情就是这样。
灰色区域是一个 UIView,并用CAShapeLayer 掩盖了它。我附加了两个UIBezierPath 并使用Even Odd Rule 删除了该灰色区域的一部分。这就是为什么它显示一条线,就好像它在左侧显示一个半圆。
红色背景只是容器视图。
现在我的问题是,当我为UIView 设置动画时,就好像在为UIView 设置动画时面具没有正确更新。
这是UIView 动画部分:
func didCompleteItem() {
self.bar.plainProgressBarLeftAnchor?.constant = self.bar.bounds.width * (self.completedItemCount*self.barMeterMultiplier) / self.bar.bounds.width - self.bar.meterCornerRadius
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
self.bar.layoutIfNeeded()
}, completion: nil)
}
现在,这里是 layoutSubviews 的一部分,如果您需要查看 CAShapeLayer 是如何创建的。
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = GradientColor(.leftToRight, frame: self.bounds, colors: GradientFlatRed())
layer.masksToBounds = true
layer.cornerRadius = meterCornerRadius
createMask()
}
var shapeLayer: CAShapeLayer = CAShapeLayer()
func createMask() {
let bPath = UIBezierPath(roundedRect: plainProgressBar.bounds, byRoundingCorners: [.topRight, .bottomRight], cornerRadii: CGSize(width: 0, height: 0))
let semiCircle = UIBezierPath(arcCenter: CGPoint(x: bounds.origin.x, y: plainProgressBar.bounds.origin.y+meterCornerRadius), radius: meterCornerRadius, startAngle: 3 * CGFloat.pi / 2, endAngle: CGFloat.pi / 2, clockwise: true)
shapeLayer.frame = layer.bounds
bPath.append(semiCircle)
shapeLayer.path = bPath.cgPath
shapeLayer.fillRule = kCAFillRuleEvenOdd
plainProgressBar.layer.mask = shapeLayer
}
【问题讨论】:
-
我不会只画一个半圆,而是画一个完整的圆来避免这个问题。另外,也许在
layoutLayer()(相当于layoutSubView)中可能有助于修复它。 -
为什么
layoutLayer()会解决它?和视图的生命周期有关系吗?
标签: ios swift uiview uiviewanimation cashapelayer