问题
我正在创造爆炸环效果。我正在使用多个CAShapeLayer
并为每个图层创建UIBezierPath
。初始化视图时,所有图层都有一个宽度为0
的路径。触发动作时,环会为更大的路径设置动画。
如下面的演示所示,每个图层的右手边缘比每个圆形图层的其余部分的动画效果要慢。
演示
代码
绘制图层:
func draw(inView view: UIView) -> CAShapeLayer {
let shapeLayer = CAShapeLayer()
// size.width defaults to 0
let circlePath = UIBezierPath(arcCenter: view.center, radius: size.width / 2, startAngle: 0, endAngle: CGFloat(360.0).toRadians(), clockwise: true)
shapeLayer.path = circlePath.CGPath
shapeLayer.frame = view.frame
return shapeLayer
}
更新图层
func updateAnimated(updatedOpacity: CGFloat, updatedSize: CGSize, duration: CGFloat) {
let updatePath = UIBezierPath(arcCenter: view.center, radius: updatedSize.width / 2,
startAngle: 0, endAngle: CGFloat(360).toRadians(), clockwise: true)
let pathAnimation = CABasicAnimation(keyPath: "path")
pathAnimation.fromValue = layer.path // the current path (initially with width 0)
pathAnimation.toValue = updatePath.CGPath
let opacityAnimation = CABasicAnimation(keyPath: "opacity")
opacityAnimation.fromValue = self.opacity
opacityAnimation.toValue = updatedOpacity
let animationGroup = CAAnimationGroup()
animationGroup.animations = [pathAnimation, opacityAnimation]
animationGroup.duration = Double(duration)
animationGroup.fillMode = kCAFillModeForwards
animationGroup.removedOnCompletion = false
layer.addAnimation(animationGroup, forKey: "shape_update")
... update variables ...
}
答案 0 :(得分:3)
避免以0的路径宽度开始。从0开始缩放并且微小的浮点误差放大是非常困难的。非常接近于零的值在浮点上不是很连续(嗯......我猜这实际上有点像真实的宇宙)。尝试从较大的值开始,看看你可以安全地接近零。