gpt4 book ai didi

ios - 延迟 CABasicAnimation 会立即绘制它

转载 作者:行者123 更新时间:2023-11-29 05:54:37 25 4
gpt4 key购买 nike

我正在尝试为一对多次分割的线制作动画。他们将有可变数量的分割,但我试图让下一个分割仅在前一个分割完成后才开始。所有的分割都是线段的末尾和下一段的开头之间的小间隙。另外,我最终会让每个片段花费一定的时间来完成,但我无法让动画延迟,以便我可以将它们链接在一起。

我正在使用Advanced Animation Tricks作为引用,但每当我打开 View Controller (它调用存储该动画的函数)时,所有线段都会立即绘制,没有动画或延迟,我不知道为什么会发生这种情况。

这是我的动画过程:

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")
}

最佳答案

尝试将 antimation.beginTime 替换为:

animation.beginTime = CACurrentMediaTime() + 5

而不是您正在使用的 currentLayerTime1

关于ios - 延迟 CABasicAnimation 会立即绘制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55287519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com