这个方法是UIView在iOS7内新增的一个可以达到回弹效果的动画。其用法很简单,但对于其中的initialSpringVelocity这个参数,我一直都没彻底搞明白,今天在认真思索+做试验后,总算可以说是彻底搞清楚了。
下面先摘抄一段官方的解释(来自UIView.h):
Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1.0, the animation will smoothly decelerate to its final model values without oscillating. Damping ratios less than 1.0 will oscillate more and more before coming to a complete stop. You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit coordinate system, where 1.0 is defined as traveling the total animation distance in a second. So if you're changing an object's position by 200pt in this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll typically want to pass 0 for the velocity.
重点请关注上面被加深的几个关键字: to behave as 、was moving at 、 before the animation started。 结合这几个关键字,我们就可以得出最后结论,那就是通过设置initialSpringVelocity,我们可以设置一种假设,而这个假设的内容,是被animate的object在animation开始之前的移动速度,并且这个假设与真实情况越接近的话,动画就会越smooth。比如view controller container在做transition的时候,假如这个transition不是通过interactive(比如手指滑动)激发的,那么在transition动画开始之前,被animate的object(就是要被移除的viewController)的移动速度通常都是0,那么这个时候,假如我们想让动画表现的尽可能的smooth的话,initialSpringVelocity就因该设为0,这就是表示当前animation所做出的动作是建立在“假设被animate的object的初始速度是0的情况下”做出来的,而这个就正好符合真实情况,所animation就会smooth。而作为对比,假如一个view controller container的transition是通过interactive,比如手指滑动,激发的话,那么这个时候被animate的object的移动速度可能就是不是0了,而是等于UIPanGestureRecognizer的velocity,这个时候如果想尽可能的使得animation顺滑得话,就应该将initialSpringVelocity的值假设为与这个object在动画发生之前的真实的移动速度相近,就像例子里所说的,假如total animation distance是200point,而object在animation开始之前的移动速度是100p/s的话,那我们就应该将initialSpringVelocity设为0.5 (因为假如设为1的话,就是说initialSpringVelocity为1秒钟能走200point,也就是说速度是200p/s,因此0.5秒的话就等于200p/s * 0.5 = 100p/s,而100p/s 就与object在animation开始之前的velocity相等了)。当然,如果你不想让动画看上去smooth的话,你可以不这么设置这个值。比如说total animation distance是100,且object在animation开始之前的velocity是0,但你将这个值设为1(也就是100p/s),那么动画就会表现的不smooth了。
鉴于本人语文水平有限,写的这么啰嗦,还请见谅。。