iOS視圖彈跳動畫

//動畫彈跳效果關鍵代碼函數

彈跳屢次可自行多嵌套幾回   調用此方法  只需傳入彈跳對象動畫

- (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"];

}

相關文章
相關標籤/搜索