如何使用贝塞尔曲线为对象设置动画?

时间:2019-07-08 09:13:39

标签: ios swift uibezierpath cgpoint cakeyframeanimation

enter image description here

这是我绘制虚线的代码

func drawLine() {

    let shapeLayer = CAShapeLayer()
    shapeLayer.bounds = viewDraw.bounds
    shapeLayer.position = CGPoint(x: viewDraw.bounds.width/2, y: viewDraw.bounds.height/2)
    shapeLayer.fillColor = nil
    shapeLayer.strokeColor = ColorConstants.ThemeColor.cgColor
    shapeLayer.lineWidth = 3
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round // Updated in swift 4.2
    shapeLayer.lineDashPattern = [10]

    let path = UIBezierPath(roundedRect: viewDraw.bounds, cornerRadius: viewDraw.bounds.size.height/2)
    shapeLayer.path = path.cgPath

    viewDraw.layer.addSublayer(shapeLayer)

    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.duration = 1
    shapeLayer.add(animation, forKey: "MyAnimation")
}

现在,按照我的屏幕截图,我想在该虚线无限时间内将蓝点动画化为红点。当蓝点到达红点时,它将再次以蓝点开始。

注意:整个屏幕都是动态的,因此画线也根据屏幕高度而动态

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您快到了

现在,您必须使用具有属性CAKeyframeAnimation的{​​{1}}添加动画,您可以在其中传递要设置动画的路径

这是工作示例代码

path

您可以查看本教程以获得更多帮助http://mathewsanders.com/animations-in-swift-part-two/

相关问题