//动画弹跳效果关键代码函数
弹跳屡次可自行多嵌套几回 调用此方法 只需传入弹跳对象动画
- (void)animationShootOut:(UIView *)animationView{spa
CGRect frame = animationView.frame;3d
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){orm
} completion:^(BOOL finished){对象
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){递归
//弹起animation
animationView.frame = CGRectMake(frame.origin.x, frame.origin.y-20, frame.size.width, frame.size.height);it
} completion:^(BOOL finished){io
[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){
}];
}];
}];
}];
}];
}
忽然发现这样太麻烦 要是想多跳几回就要写不少了,因此刚刚又封装了一下 就是感受跳动不流畅 太生硬 暂时没什么思路解决
封装的就是用一个C 函数递归调用 感受还行
//弹射动画函数
void shootOutAnimation(UIView *view ,int n,float height,CGRect frame,int num) {
//n 为弹跳的次数 若是传入的是一个基数 那么就会自动默认加一为偶数
//height 为第一次弹跳的最高高度 20
//view 为传入的视图
//frame 为传入视图的坐标
//num 必须和传入的弹跳次数一致
if (n<0) {
return;
}
n = n-1;
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
view.frame = CGRectMake(frame.origin.x, frame.origin.y-((n%2)?0:(n+2)*(height/num)), frame.size.width, frame.size.height);
} completion:^(BOOL finished){
shootOutAnimation(view, n, height, frame,num);
}];
}
今天又发现另一方法 mark
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"];
}