Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to monitor elapsedTime #4

Closed
wants to merge 3 commits into from

Conversation

ckentgeorge
Copy link

I'm not sure if this is the preferred way, but we needed a way to detect when an animation was complete, and comparing the elapsed time to the duration accomplished this for us. The only change is to make Animation's elapsedTime property publicly readable.
If you would prefer a different approach, please make a suggestion and I'd be happy to work it up.

@ckentgeorge ckentgeorge changed the title Ability to monitor duration Ability to monitor elapsedTime Sep 26, 2017
@SteveBarnegren
Copy link
Owner

@ckentgeorge I don't think there's any harm in making the Animation's elapsed time public. There is already a way of being notified when an action finishes though:

If you create an ActionSequence and pass in your action, followed by a RunBlockAction, you can use the RunBlockActions closure as a callback for when your action finishes. Something like this:

let yourAction = // make your action

let callbackAction = RunBlockAction(handler: {
    // Animation finish callback
})

let sequence = ActionSequence(actions: [yourAction, callbackAction])

The advantage that this has over checking the elapsed duration is that this will always be called, even in the event of a frame rate drop resulting in the update method of the action being completely skipped over, or was not called towards the end of the action. (eg. you might test if the elapsed time is within 0.02 seconds of the duration, but in the event of a frame rate drop at that point, update may never be called with that value).

There is also another mechanism that you may be able to use, depending on your use case. Each action has a willBecomeActive and didBecomeActive closure that you can set:

public var onBecomeActive: () -> () = {}
public var onBecomeInactive: () -> () = {}

The subtle difference here is that these closures are just called to inform you of the active/inactive state of the action. If you run the action in reverse, for instance, onBecomeActive will still be called first, and onBecomeInactive after the action has finished.

In the first example, using the RunBlockAction, if you run the action in reverse, the ActionSequence will call the RunBlockAction first, and then your action second, because it maintains the order of its inner actions, which are now reversed.

Can you let me know if that solves your issue? I'm inclined to keep the duration private for now, but if you have a use case that isn't satisfied, then it would be good to find a solution.

I should also try to spend a bit more time on documentation too!

@SteveBarnegren
Copy link
Owner

@ckentgeorge Did you manage to solve your issue using the current TweenKit api?

@ckentgeorge
Copy link
Author

ckentgeorge commented Oct 25, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants