首页 > 代码库 > [翻译] JNWSpringAnimation

[翻译] JNWSpringAnimation

JNWSpringAnimation

https://github.com/jwilling/JNWSpringAnimation

 

JNWSpringAnimation is a subclass of CAKeyframeAnimation that adds support for creating damped harmonic animations.

JNWSpringAnimation 是 CAKeyframeAnimation 的子类,它支持创建阻尼动画效果。

 

Getting Started

Although JNWSpringAnimation is a subclass of CAKeyframeAnimation, it should be treated as if it were a subclass of CABasicAnimation.

尽管,JNWSpringAnimation 是 CAKeyframeAnimation 的子类,但是,你应该把他当做 CABasicAnimation 的子类来使用:)。

 

To get started, copy the four source files into your project.

把4个源文件拷贝到你的工程当中。

 

The animation can be created by using the dedicated initializer, +animationWithKeyPath:. Alternatively, the animation can be created using +animation and by setting the key path afterwards. The full list of currently-compatible animatable properties is available in the header.

你可以使用专用的初始化方法 +animationWithKeyPath:。当然,你也可以使用 +animation 方法,之后再来设置 keyPath 。目前所有兼容的属性动画都包含在头文件当中。

JNWSpringAnimation *animation = [JNWSpringAnimation animationWithKeyPath:@"position.x"];

Next, the fromValue and toValue properties must be set for the interpolated values to be calculated correctly.

下一步,fromeValue 与 toValue 属性需要插入正确的值哦。

animation.toValue = @(toX);
animation.fromValue = @(currentX);

 

Finally, the values for the spring constants can optionally be changed. Currently, stiffnessdamping, andmass are available for modification.

最后,阻尼动画的一些参数也可以修改,目前包括 stiffness(生硬程度),damping(衰弱程度?)以及mass(质量)。

animation.mass = 30; // this will move extremely slowly
// and so on

 

The animation itself can be applied like any other subclass of CAAnimation, namely -addAnimation:forKey: on any CALayer.

这个动画本身可以被任何CAAnimation的子类接收,你可以将他添加到CALayer中。

 

What‘s this for?

This was created in my desire to have an open-source version of the (currently private) CASpringAnimationwhich was discovered in iOS 6.

Spring animations, when used appropriately, can really enhance the way your app feels to the user by connecting in physical simulations with an app‘s interface. And besides, who doesn‘t love messing around with springs?

 

What‘s left to do?

I created this as a weekend project, so the implementation is still somewhat unfinished. Compared toCASpringAnimation, the velocity property is still unimplemented. Pull requests are welcome.

这只是我的小项目中的一部分,所以有些东西还不完善。