Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Swift 4.2 :

//MARK:- UIDynamicAnimator Delegate
extension YourViewController: UIDynamicAnimatorDelegate {
    func dynamicAnimatorDidPause(_ animator: UIDynamicAnimator) {
        // pause
    }

    func dynamicAnimatorWillResume(_ animator: UIDynamicAnimator) {
        // resume
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top