iOS視圖彈跳動畫
//動畫彈跳效果關鍵代碼
彈跳多次可自行多嵌套幾次 調用此方法 只需傳入彈跳對象
- (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){
}];
}];
}];
}];
}];
}
突然發現這樣太麻煩 要是想多跳幾次就要寫很多了,所以剛剛又封裝了一下 就是感覺跳動不流暢 太生硬 暫時沒什么思路解決
封裝的就是用一個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);
}];
}