Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中绘制圆的动画中的奇怪问题_Ios_Swift_Animation_Uibezierpath_Cashapelayer - Fatal编程技术网

Ios 在Swift中绘制圆的动画中的奇怪问题

Ios 在Swift中绘制圆的动画中的奇怪问题,ios,swift,animation,uibezierpath,cashapelayer,Ios,Swift,Animation,Uibezierpath,Cashapelayer,我正在尝试用以下动画绘制一个圆: func drawProgressCircle(with endAngle: CGFloat) { let progressBezierPath = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.width / 2), radius: self.frame.width / 2.5, startAngle: -CGFloat(Double.pi / 2), en

我正在尝试用以下动画绘制一个圆:

func drawProgressCircle(with endAngle: CGFloat) {
    let progressBezierPath = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.width / 2), radius: self.frame.width / 2.5, startAngle: -CGFloat(Double.pi / 2), endAngle: endAngle, clockwise: true)
    let progressShapeLayer = CAShapeLayer()
    progressShapeLayer.path = progressBezierPath.cgPath

    progressShapeLayer.strokeColor = UIColor.brown.cgColor
    progressShapeLayer.fillColor = UIColor.clear.cgColor
    progressShapeLayer.lineWidth = 15.0

    // Do not draw initially. Wait for animation
    progressShapeLayer.strokeEnd = 0.0

    // Make corners rounded
    progressShapeLayer.lineCap = kCALineCapRound

    self.layer.addSublayer(progressShapeLayer)
    self.animate(circleWith: progressShapeLayer, from: -CGFloat(Double.pi / 2), to: endAngle, with: 2.0)
}
要使其动画化,我将此函数称为:

func animate(circleWith shapeLayer: CAShapeLayer, from: CGFloat, to: CGFloat, with duration: TimeInterval) {
    // We want to animate the strokeEnd property of the circleLayer
    let animation = CABasicAnimation(keyPath: "strokeEnd")

    // Set the animation duration appropriately
    animation.duration = duration

    // Animate from start point to end point
    animation.fromValue = from
    animation.toValue = to

    // Do a linear animation (i.e. the speed of the animation stays the same)
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
    // right value when the animation ends.
    shapeLayer.strokeEnd = 1.0

    // Do the actual animation
    shapeLayer.add(animation, forKey: "animate")
}
但我不知道为什么它没有动画。画布是空的,2秒钟后它就会出现在画布上。没有任何动画


有什么问题?为什么不设置图形动画?

将动画formValue和toValue替换为:

// Animate from start point to end point
animation.fromValue = 0
animation.toValue = 1
基本上,关键路径
笔划的
CABasicAnimation
不知道您正在绘制一个圆,它只是从0到1设置动画。 如果将
toValue
的值设为0.5,则它将仅对路径的一半设置动画

它不同于键路径
位置的
cabasicanitation
,键路径
位置为
fromValue
toValue
取CGPoint