方法一:
如果需弹跳多次,动画要多层嵌套,代码量稍微有点大,弹跳结束中心点不变。
//动画弹跳效果关键代码
弹跳多次可自行多嵌套几次 调用此方法 只需传入弹跳对象
- (void)animationShootOut:(UIView *)animationView{
CGRect frame = animationView.frame;
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
//弹起
animationView.frame = CGRectMake(frame.origin.x, frame.origin.y-20, frame.size.width, frame.size.height);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
//下降
animationView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
//弹起
animationView.frame = CGRectMake(frame.origin.x, frame.origin.y-10, frame.size.width, frame.size.height);
} completion:^(BOOL finished){
//下降
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
animationView.frame = frame;
} completion:^(BOOL finished){
}];
}];
}];
}];
}];
}
方法二:
usingSpringWithDamping:弹簧动画的阻尼值,也就是相当于摩擦力的大小,该属性的值从0.0到1.0之间,越靠近0,阻尼越小,弹动的幅度越大,反之阻尼越大,弹动的幅度越小,如果大道一定程度,会出现弹不动的情况。
initialSpringVelocity:弹簧动画的速率,或者说是动力。值越小弹簧的动力越小,弹簧拉伸的幅度越小,反之动力越大,弹簧拉伸的幅度越大。这里需要注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果,弹跳结束后中心点下移10
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:4 options:UIViewAnimationOptionAllowUserInteraction animations:^{
CGPoint tempPoint=_btn.center;
tempPoint.y += 10;
_btn.center=tempPoint;
} completion:^(BOOL finished) {
}];
方法三:
弹跳结束后中心点不变,推荐使用此方法。
// _btn调用动画效果
shakerAnimation(_btn, 2, 20);
// 重力弹跳动画效果
void shakerAnimation (UIView *view ,NSTimeInterval duration,float height){
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
CGFloat currentTx = view.transform.ty;
animation.duration = duration;
animation.values = @[@(currentTx), @(currentTx + height), @(currentTx-height/3*2), @(currentTx + height/3*2), @(currentTx -height/3), @(currentTx + height/3), @(currentTx)];
animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:animation forKey:@"kViewShakerAnimationKey"];
}