问题
我正在创造爆炸环效果.我正在使用多个CAShapeLayer并为每个层创建一个UIBezierPath.初始化视图时,所有图层都有一个宽度为0的路径.触发动作时,环会为更大的路径设置动画.
我正在创造爆炸环效果.我正在使用多个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,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开始.从0开始缩放并且微小的浮点误差放大是非常困难的.非常接近零的值在浮点上不是很连续(嗯……我猜这实际上有点像真实的宇宙).尝试从较大的值开始,看看你可以安全地接近零.