【问题标题】:Delaying a CABasicAnimation instantly draws it延迟 CABasicAnimation 立即绘制它
【发布时间】:2019-03-21 18:59:19
【问题描述】:

我试图为一对分割多次的线设置动画。他们将有可变数量的拆分,但我试图让下一个拆分仅在前一个拆分完成后才开始。所有的分割都是一条线段的结束和下一段的开始之间的一个小间隙。此外,我最终会让每个片段花费特定的时间来完成,但我不能让动画延迟,所以我可以将它们链接在一起。

我使用Advanced Animation Tricks 作为参考,但是每当我打开视图控制器时,它会调用存储此动画的函数,所有线段都会立即绘制,没有动画或延迟,我不知道为什么会这样.

这是我的动画制作过程:

for i in 0...(splits.count - 1)
{
    //create path
    //start buffered after split
    //finish buffered before split
    let path1 = UIBezierPath()
    path1.move(to: CGPoint(x: lineSplits[i] + 5, y: lineLoc[0] - CGFloat(lw/2)))
    path1.addLine(to: CGPoint(x: lineSplits[i] + 5, y: lineLoc[0] - CGFloat(lw/2)))
    path1.addLine(to: CGPoint(x: lineSplits[i + 1] - 5, y: lineLoc[0] - CGFloat(lw/2)))

    let path2 = UIBezierPath()
    path2.move(to: CGPoint(x: lineSplits[i] + 5, y: lineLoc[0] + CGFloat(lw/2)))
    path2.addLine(to: CGPoint(x: lineSplits[i] + 5, y: lineLoc[0] + CGFloat(lw/2)))
    path2.addLine(to: CGPoint(x: lineSplits[i + 1] - 5, y: lineLoc[0] + CGFloat(lw/2)))

    // create shape layer for that path
    let shapeLayer1 = CAShapeLayer()
    shapeLayer1.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
    shapeLayer1.strokeColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
    shapeLayer1.lineWidth = CGFloat(lw)
    shapeLayer1.path = path1.cgPath

    let shapeLayer2 = CAShapeLayer()
    shapeLayer2.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
    shapeLayer2.strokeColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1).cgColor
    shapeLayer2.lineWidth = CGFloat(lw)
    shapeLayer2.path = path2.cgPath

    // animate it with a delay
    let currentLayerTime1 = shapeLayer1.convertTime(CACurrentMediaTime(), from: nil)
    let currentLayerTime2 = shapeLayer2.convertTime(CACurrentMediaTime(), from: nil)

    view.layer.addSublayer(shapeLayer1)
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.beginTime = currentLayerTime1 + 5
    animation.duration = 5
    animation.fillMode = CAMediaTimingFillMode.backwards
    shapeLayer1.add(animation, forKey: "MyAnimation")


    view.layer.addSublayer(shapeLayer2)
    let animation2 = CABasicAnimation(keyPath: "strokeEnd")
    //animation2.fromValue = 0
    animation2.beginTime = currentLayerTime2 + 10
    animation2.duration = 10
    animation2.fillMode = CAMediaTimingFillMode.backwards
    shapeLayer2.add(animation2, forKey: "MyAnimation")
}

【问题讨论】:

    标签: ios swift xcode cabasicanimation


    【解决方案1】:

    尝试将antimation.beginTime 替换为:

    animation.beginTime = CACurrentMediaTime() + 5
    

    而不是您正在使用的currentLayerTime1

    【讨论】:

    • 很遗憾,同样的问题,没有任何变化
    • 我修好了!我添加了一行:animation.fromValue = 0 解决了它的每个问题,我发誓我之前尝试过,但无济于事,感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多