RBBAnimation icon indicating copy to clipboard operation
RBBAnimation copied to clipboard

Double the RBBSpringAnimation "sampling rate" for smoother animations.

Open orj opened this issue 9 years ago • 4 comments

While using RBBSpringAnimation I noticed that movement was quite jerky in the initial phases of the spring animation.

To combat this I increased the "sampling rate" specified in RBBAnimation's values method from 60 to 120. Eg:

- (NSArray *)values {
    RBBAnimationBlock block = [self.animationBlock copy];

    CFTimeInterval duration = self.duration;

    return [RBBBlockBasedArray arrayWithCount:(NSUInteger)(duration * 120) block:^id(NSUInteger idx) {
        return block(idx / 120.0f, (CGFloat)duration);
    }];
}

The results seem to be smoother animations.

I'm not a math/signal processing whiz but I think this is an example of the Nyquist rate.

Perhaps this could be made a property on RBBSpringAnimation. As in some situations I think 60 would be fine but others a higher value is going to produce smoother results.

orj avatar Jul 01 '15 08:07 orj

That explanation seems reasonable to me, thanks :sparkling_heart:

Do you happen to have an example project at hand that shows an example of this behavior?

robb avatar Jul 01 '15 12:07 robb

I just hacked together a sample but I can't attach to issue (because Github doesn't support zips). I'll throw it in a git repo.

With this sample I think I also ran into another solution to the smoothness issue. Changing the calculation mode to linear from discrete.

orj avatar Jul 01 '15 14:07 orj

I've pushed up a sample to https://github.com/orj/RBBAnimation-SmootherSpring

The sample lets you control the animation with the slider. Once the app loads into the iOS simulator hit the "setup" button then you can control the time with the slider. You will see a default of 60 leads to big jumps at the beginning of the animation for the red box but the blue box is smoother with 120 keyframes.

Set the segmented control to linear and hit setup again to see how kCAAnimationLinear calculationMode effects the animation.

orj avatar Jul 01 '15 14:07 orj

Oh, to get it to build don't forget to git submodule update --init --recursive in the clone of my repo to pull down your animation lib as a submodule.

orj avatar Jul 01 '15 14:07 orj