Layer-Springs

来源:互联网 发布:c 编程例子 编辑:程序博客网 时间:2024/06/03 20:43

Introduction

Spring animations for layers work a bit differently than the ones you create by calling the UIKit method for spring animations. The UIKit method lets you create a somewhat oversimplified(过于简单的) spring-like animation, but it’s Core Animation counterpart(相对应的) renders a proper physical simulation that looks and feels much more natural.(与之相对应的 Core Animation 可以提供一种看上去更加自然真实的效果).

The UIKit API simplified the creation of spring animations; you didn’t need to know much about how the worked under the hood. However,since you’re a Core Animation expert now,you’ll be expected to delve(钻研,深究) a bit deeper into the details.

When we see a pendulum swing and put a pice of paper underneath ,we will see the following picture:
这里写图片描述
the pendulum will slow down by a little bit each time until it comes to rest.The length of time it takes the pendulum to settle down, and ultimately the way the graph(图表) of the oscillator looks,depends on the following parameters of the oscillating system:

  • Any damping(阻尼因数): this is due to air friction,mechanical firction and other external slowing forces acting on the system 这取决于空气阻力,机械摩擦力和其他额外的因素.
  • The mass(质量): the heavier the pendulum,the greater the length of time it will swing. 钟摆质量越高,变慢的速度越低
  • The stiffness(刚度): the stiffer the “spring” of the oscillator ,whick in this case is Earth’s gravity ,the harder the pendulum will swing at first,and the faster the system will settle down. Imagine if you were to user this pendulum on the moon or on Jupiter; the movements in low and high gravity situations would be quite different.(刚度,在这个例子中即地球引力。引力越大,钟摆第一次就摆的更用力,停止的就越快。假如是在月球或者木星,情况会大不相同)
  • The initial velocity (初始速度)

All the above calls Damped Harmonic Oscillator(阻尼谐振,阻尼谐振动),which drives the spring animations in iOS.

UIKit vs. Core Animation springs

UIKit adjusts some variables in a dynamic manner to make the system settle down in the giver duration.(UIKit 以动态的方式调整一些变量,使得系统在一定的时间内安定下来)
That’s why the UIKit spring animations sometimes feel a bit forced(别扭hah). UIKit animations are just a bit too jumpy, and to a traine eye ,a tad unnatural.
Luckily ,Core Animation lets you create proper spring animations for your layer properties vis the CASpringAnimation class.
CASpringAnimation creates the spring animations for UIKit behind the scenes,but when you call it directly you can set the various variables of the system and let the animation settle down by itself.
The drawback to this approach is that you can’t tell the animation what its duration shoule be; that’s determined by the system itself, given the variables you provide.(缺点之处在于你不能控制动画的持续时间,而是系统根据你提供的参数自己计算的)
Since you know how damped harmonic oscillators work,it’s no surprice that CASpringAnimation exposes the following properties

  • damping - the damping aoolied to the system
  • mass - the mass of the weight in the system
  • stiffness - the stiffness of the spring attached to the weight.
  • initialVelocity - the initial push applied to the weight.
    If at any time you’re wondering which variables you need to adjust to get your animation working the way you want,simply think back to the grandfather clock example and that should get you straightened around.
    That’s enough prep for now - it’s time to open up Xcode and lay down some spring animation code.
let pulse = CASpringAnimation(keyPath: "transform.scale")pulse.damping = 2.0pulse.duration = pulse.settlingDuration

Once you set all system variables such as stiffness and damping, ask your CASpringAnimation how much time it will take to settle down and set that as the duration of the animation.

Spring animation properties

That takes care of your first spring animation; all you had to do was adjust the damping and everything worked itself out.
CASpringAnimation comes with pre-defined values for all its springy properties:
Property Default Value
damping 10.0
mass 1.0
stiffness 100.0
initialVelocity 0.0

0 0
原创粉丝点击