如何在Swift中使用Core Animation制作链接动画?

问题描述 投票:0回答:1

我想先做animation1,然后再做animation2。我也想永远重复一遍。

let animation1 = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
animation1.path = circularFirstPath.cgPath

let animation2 = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
animation2.path = circularSecondPath.cgPath

circleView3.layer.add(animation1, forKey: nil)
circleView3.layer.add(animation2, forKey: nil)
swift xcode uikit core-animation xcode11
1个回答
0
投票

如果您想更改位置,请尝试以下操作:

    let segmentDuration: CFTimeInterval = 2.0

    let points = [CGPoint(x: 0, y: 0), CGPoint(x: 400.0, y: 0.0), CGPoint(x: 200.0, y: 200.0)]

    let posAnimation = CABasicAnimation(keyPath: "position")
    posAnimation.duration = segmentDuration
    posAnimation.fromValue = points[0]
    posAnimation.toValue = points[1]

    let posAnimation1 = CABasicAnimation(keyPath: "position")
    posAnimation1.beginTime = posAnimation.duration
    posAnimation1.duration = segmentDuration
    posAnimation1.fromValue = points[1]
    posAnimation1.toValue = points[2]

    let posAnimation2 = CABasicAnimation(keyPath: "position")
    posAnimation2.beginTime = posAnimation.duration + posAnimation1.duration
    posAnimation2.duration = segmentDuration
    posAnimation2.fromValue = points[2]
    posAnimation2.toValue = points[0]

    let positionChain: CAAnimationGroup = CAAnimationGroup()
    positionChain.animations = [posAnimation, posAnimation1, posAnimation2]
    positionChain.duration = posAnimation.duration + posAnimation1.duration + posAnimation2.duration
    positionChain.fillMode = CAMediaTimingFillMode.forwards
    positionChain.repeatCount = .infinity
    anySubLayer.add(positionChain, forKey: "positionChain")
© www.soinside.com 2019 - 2024. All rights reserved.