Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 993
Undo Avatar asked Mar 08 '14 00:03

Undo


2 Answers

After some more poking around, it seems that the UIDynamicAnimatorDelegate 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.

like image 73
Undo Avatar answered Oct 06 '22 22:10

Undo


The animator has a property "running" that tells you when the animator is at rest.

The views associated with an animator’s behaviors can change position or change transform only when the animator is running. For optimization purposes, iOS can pause and then restart an animator. Use this method if you need to check whether or not your views are currently subject to changes in position or transform.

like image 25
Axy Avatar answered Oct 06 '22 22:10

Axy