抛物线动画

动画管理类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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容