假如要对一个view进行一种animation,使得这个view往下移动一段距离,弹一弹然后停住,就要利用POPSpringAnimation这个类。
很大程度上参考了popping那份代码。感谢
直接上代码:
-(void)moveDownView:(UIView *)view
{
/*
kPOPLayerPosition意思是这个animation对象要干的事情是移动layer的Position
还有很多其他类型的spring animation
kPOPLayerOpacity 透明度渐变
kPOPLayerRotation 旋转渐变
kPOPLayerScaleXY 大小渐变
kPOPLayerTranslationXY 仿射变换渐变
等等等等。。实在太多不宜列举
*/
POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
/*
toValue是设置变化后的参数,例如PositionAnimation的话,toValue就是终点的坐标
1.springBounciness 弹簧弹力 取值范围为[0, 20],默认值为4
2.springSpeed 弹簧速度,速度越快,动画时间越短 [0, 20],默认为12,和springBounciness一起决定着弹簧动画的效果
3.dynamicsTension 弹簧的张力
4.dynamicsFriction 弹簧摩擦
5.dynamicsMass 质量 。张力,摩擦,质量这三者可以从更细的粒度上替代springBounciness和springSpeed控制弹簧动画的效果
*/
positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(view.center.x,view.center.y+100)];
positionAnimation.springSpeed = 1.0f;
positionAnimation.springBounciness = 20.0f;
/*
设置好参数后,就可以用函数pop_addAnimation: forKey来开始animation。这里的key应该是记录这次animation的设置,保存到某个堆栈,在需要的时候重新调用出来。
*/
[view.layer pop_addAnimation:positionAnimation forKey:@"layerPositionAnimation"];
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.5, 0.5)];
scaleAnimation.springBounciness = 10.f;
[view.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}
调用这个函数,就可以使得view开始animation,例如配合一个touchUpInside
- (void)touchUpInside:(UIControl *)sender {
AnimationInfo animationInfo = [self animationInfoForLayer:sender.layer];
BOOL hasAnimations = sender.layer.pop_animationKeys.count;
if (hasAnimations && animationInfo.progress < 0.98) {
[self pauseAllAnimations:NO forLayer:sender.layer];
return;
}
[sender.layer pop_removeAllAnimations];
[self moveDownView:sender];
}
特别备注一:
测试了几组参数,先描述如下
springSpeed | springBounciness | description |
1 | 20 | 总体速度缓慢,振动比较明显 |
1 | 10 | 总体速度缓慢,振动变得不明显, |
1 | 5 | 总体速度缓慢,振动非常不明显。 |
5 | 20 | 速度提升,振动明显 |
15 | 20 | 速度相当快,振动明显 |