Undo Undo - 6 months ago 23
iOS Question

How can I tell when a UIDynamicAnimator is at rest?

I'm playing with UIKitDynamics, and it's really neat. Currently I have this code:

CGPoint center = [newSelectedLabel center];

[self.animator removeBehavior:self.snapBehavior];
self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.indicatorView snapToPoint:center];
self.snapBehavior.damping = 0.67;
[self.animator addBehavior:self.snapBehavior];

[self.delegate didChangeToIndex:sender.tag];


It's a simple snap behavior, and it works quite well. For various reasons, though, I want to know when the system goes to a 'rest' state - i.e. everything stops moving. I'm fine with either a property on the animator, or a delegate method.

How can I do this?

Answer

After some more poking around, it seems that the UIKitDynamicAnimatorDelegate does this - the - (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator method seems to be called when the system is at a rest state.

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
self.animator.delegate = self;

and

#pragma mark - UIDynamicAnimator Delegate
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator
{
    NSLog(@"pause");
}
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator
{
    NSLog(@"resume");
}

seem to work - resume is logged when the animation starts, and pause is logged within a second of the animation stopped.

Additionally, the running property on the UIDynamicAnimator itself seems to mirror the calls to the delegate methods - it's 1 when willResume is called, and it's 0 when didPause is called.

Comments