Swift:CAShapeLayer上路径的动画位置

时间:2017-07-31 14:16:29

标签: swift

我有一个路径和一个形状图层,我想绘制一条线

let path = UIBezierPath()
path.move(to: CGPoint(x: xValue,y: yValue))
path.addLine(to: CGPoint(x: xValue, y: yValue + 300))

// create shape layer for that path

let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1).cgColor
shapeLayer.lineWidth = 3
shapeLayer.path = path.cgPath

但我也在动画画线

self.chartView.layer.addSublayer(shapeLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.duration = 0.6
shapeLayer.add(animation, forKey: "MyAnimation")

现在我的问题: 我也希望同时为我的路径设置动画。所以目前我的线路位于" xValue"的x-Positon上。我想将它动画移动到" xValue - 40.0"。我尝试了两种方法都没有用。

  1. 使用UIView.animate。问题:x位置正在改变但没有动画:

    UIView.animate(withDuration: 3.0) {
        var highlightFrame = shapeLayer.frame
        highlightFrame.origin.x = (highlightFrame.origin.x) - 40
        shapeLayer.frame = highlightFrame
    }
    
  2. 在形状图层上添加动画。这里什么都没发生

    let toPoint:CGPoint = CGPoint(x: -40.0, y: 0.0)
    let fromPoint:CGPoint = CGPoint.zero
    let movement = CABasicAnimation(keyPath: "movement")
    movement.isAdditive = true
    movement.fromValue = NSValue(cgPoint: fromPoint)
    movement.toValue = NSValue(cgPoint: toPoint)
    movement.duration = 0.3
    shapeLayer.add(movement, forKey: "move")
    
  3. 有人有另一种解决方案吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

chart.scrollbarX.series.events.on("removed", function(removed){
  chart.scrollbarX.scrollbarChart.series.removeValue(removed.oldValue.clones.getIndex(0));
});