Question

I have some UIViews that are using UIDynamicAnimation, specifically UIGravityBehavior. I need to know how to make them stop and start whenever I want to.

I have been trying all sorts of combinations of manually setting the properties of the behavior, like setting the gravity to 0, 0, or separately setting the magnitude and the vector to 0, or even setting all that plus the angle to 0 all at once, and the things just keep on moving.

I can sort of move them to a different location, but they just retain the momentum they had before and keep moving.

The last thing I can think of is to remove the views from the behavior altogether, but I'm going to need to be stopping and starting a lot of different times, and that seems very very clunky. Is that the only way to do this?

Was it helpful?

Solution

the method:

[aView removeMotionEffect:effect];

is optimized, so you should not fear from executing it. My guess is that 'playing' with their vectors & gravity, while managing a lot of view will be far more difficult then just removing their behaviors.

you can use this snippet:

- (void)removeMotionEffectsFromViews:(NSArray *)views
{
     for (id obj in views) {

        if ([obj isKindOfClass:[UIView class]]) {

            UIView *aView = (UIView *)obj;

            for (UIMotionEffect *effect in [aView motionEffects]) {
                [aView removeMotionEffect:effect];
            }
        }
    }

    [self.animator /*UIDynamicAnimator*/ removeAllBehaviors];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top