美文网首页
iOS-图标上下跳动的3种方法

iOS-图标上下跳动的3种方法

作者: Simple_Code | 来源:发表于2018-07-04 14:16 被阅读525次

    方法一:使用1,2会出现卡顿

    1.推荐使用

    - (void)ImageSpring {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.frame = CGRectMake(_imageView.frame.origin.x, _imageView.frame.origin.y+10, 40, 40);
        }];
        
        [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _imageView.frame = CGRectMake(_imageView.frame.origin.x, _imageView.frame.origin.y-10, 40, 40);
        } completion:^(BOOL finished) {
            [self ImageSpring];
        }];
    } 
    
    [UIView animateWithDuration:0.2f delay:CGFLOAT_MIN options:0 animations:^(void){
            //弹起
            animationView.top = animationView.top - 15;
        } completion:^(BOOL finished){
            [UIView animateWithDuration:0.2f delay:CGFLOAT_MIN options:0 animations:^(void){
                //下降
                animationView.top = animationView.top + 15;
            } 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"];
    }
    

    方法三:

    - (void)popJumpAnimationView:(UIView *)sender {
        
        CGFloat duration = 0.4f;
        CGFloat height = 25.f;
        CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
        CGFloat currentTy = sender.transform.ty;
        animation.duration = duration;
        animation.values = @[@(currentTy), @(currentTy - height/4), @(currentTy-height/4*2), @(currentTy-height/4*3), @(currentTy - height), @(currentTy-height/4*3), @(currentTy -height/4*2), @(currentTy - height/4), @(currentTy)];
        animation.keyTimes = @[ @(0), @(0.025), @(0.085), @(0.2), @(0.5), @(0.8), @(0.915), @(0.975), @(1) ];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.repeatCount = 1;
        [sender.layer addAnimation:animation forKey:@"kViewShakerAnimationKey"];
    }
    

    相关文章

      网友评论

          本文标题:iOS-图标上下跳动的3种方法

          本文链接:https://www.haomeiwen.com/subject/oyyeuftx.html