Dilshod Turobov
Dilshod Turobov

Reputation: 127

How to perform action on RealityKit animation completion

In WWDC19 talk they showed how to animate Entity and perform action on animation completion (go to minute 12 for reference) like this:

let animationController = move(to: flipUpTransform, relativeTo: parent, duration: 0.25, timingFunction: .easeInOut)

animationController.completionHandler {
// Perform some action
}

But it looks like that they removed or never added this completionHandler. Instead they have isCompleted boolean property. I am new to swift so I have no idea to use this isCompleted to perform some actions upon animation completion. How can I solve this?

Upvotes: 1

Views: 1416

Answers (1)

jlsiewert
jlsiewert

Reputation: 3554

The documentation says:

Look for one of the events in AnimationEvents if you want to be alerted when certain aspects of animation occur.

So you can use Combine to get notified when the animation finishes. so

let animation = entity.move(to: targetTransform, relativeTo: nil, duration: 1)

arView.scene.publisher(for: AnimationEvents.PlaybackCompleted.self)
       .filter { $0.playbackController == animation }
       .sink(receiveValue: { event in
            / * your completion handler */
        }).store(in: &subscriptions)

Upvotes: 2

Related Questions