假如要對一個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 |
速度相當快,振動明顯 |