美文网首页
抛物线动画

抛物线动画

作者: 蛋白质corn | 来源:发表于2018-08-14 10:52 被阅读0次

    动画管理类Animal

    /**

     *  开始动画

     *

     *  @param view        添加动画的view

            view为button时    _layer.contents = (__bridge id)view.imageView.image.CGImage

             view 为imageView时    _layer.contents = view.layer.contents

     *  @param rect        view 的绝对frame

     *  @param finishPoint 动画终点位置

     *  @param animationFinisnBlock 动画完成回调

     */

    -(void)startAnimationandView:(UIButton*)view andRect:(CGRect)rect andFinisnRect:(CGPoint)finishPoint andFinishBlock:(animationFinisnBlock)completion{

     //图层

        _layer= [CALayerlayer];

        _layer.contents = view.layer.contents;

        _layer.contentsGravity = kCAGravityResizeAspect;

        // 改变做动画图片的大小

        rect.size.width=40;

        rect.size.height=40;  //重置图层尺寸

        _layer.bounds= rect;

        _layer.cornerRadius = rect.size.width/2;

        _layer.masksToBounds=YES;          //圆角

        AppDelegate*delegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;

        [delegate.window.layeraddSublayer:_layer];

        _layer.position=CGPointMake(rect.origin.x+view.frame.size.width/2,CGRectGetMidY(rect));//开始点

        // 路径

        UIBezierPath *path = [UIBezierPath bezierPath];

        [pathmoveToPoint:_layer.position];

        //确定抛物线的最高点位置  controlPoint

        [pathaddQuadCurveToPoint:finishPoint controlPoint:CGPointMake(ScreenWidth/2+100 , rect.origin.y-80)];

        //关键帧动画

        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

        pathAnimation.path= path.CGPath;

        // pathAnimation.delegate = self;

        //往下抛时旋转小动画

        CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

        rotateAnimation.removedOnCompletion=YES;

        rotateAnimation.fromValue= [NSNumbernumberWithFloat:0];

        rotateAnimation.toValue= [NSNumbernumberWithFloat:12];

        /**

         *  kCAMediaTimingFunctionLinear  动画从头到尾的速度是相同的

         kCAMediaTimingFunctionEaseIn  动画以低速开始。

         kCAMediaTimingFunctionEaseOut  动画以低速结束。

         kCAMediaTimingFunctionEaseInEaseOut  动画以低速开始和结束。

         kCAMediaTimingFunctionDefault

         */

        rotateAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        CAAnimationGroup *groups = [CAAnimationGroup animation];

        groups.animations=@[pathAnimation,rotateAnimation];

        groups.duration=1.2f;

        //设置之后做动画的layer不会回到一开始的位置

        groups.removedOnCompletion=NO;

        groups.fillMode=kCAFillModeForwards;

        //让工具类成为组动画的代理

        groups.delegate=self;

        [_layer addAnimation:groups forKey:@"1"];

        if(completion) {

            _animationFinisnBlock= completion;

        }

    }

    //动画完成后代理

    - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag

    {

        //    [anim def];

        if(anim == [_layeranimationForKey:@"1"]) {

            [_layer removeFromSuperlayer];

            _layer=nil;

            if (_animationFinisnBlock) {

                _animationFinisnBlock(YES);

            }

        }

    }

    //上下抖动动画

    +(void)shakeAnimation:(UIView*)shakeView

    {

        CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

        shakeAnimation.duration=0.25f;

        shakeAnimation.fromValue= [NSNumbernumberWithFloat:-5];

        shakeAnimation.toValue= [NSNumbernumberWithFloat:5];

        shakeAnimation.autoreverses=YES;

        [shakeView.layeraddAnimation:shakeAnimationforKey:nil];

    }

    相关文章

      网友评论

          本文标题:抛物线动画

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