美文网首页『ios』进阶
『ios』动画篇-购物车动画

『ios』动画篇-购物车动画

作者: butterflyer | 来源:发表于2018-07-03 18:25 被阅读29次

    先来看下效果。

    QQ20180703-182009-HD.gif
    
    +(instancetype)shareTool{
        return [[PayCarAnimationTools alloc]init];
    }
    
    -(void)startAnimationWithView:(UIView *)view startRect:(CGRect)rect endPoint:(CGPoint)endPoint finishBlock:(animateFinishBlock)completion{
        _layer = [CALayer layer];
        _layer.contents = view.layer.contents;//把view里面的内容赋值给这个layer的content。
        _layer.contentsGravity = kCAGravityResizeAspectFill;//这个属性是NSString类型,它和UIView的contentMode一样,目的是为了处理内容在图层的边界内如何对齐
        _layer.bounds = rect;
        _layer.cornerRadius = rect.size.width/2;
        _layer.masksToBounds = YES;
        
        
        
        UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
        [keywindow.layer addSublayer:_layer];
        _layer.position = CGPointMake(rect.origin.x+view.frame.size.width/2, CGRectGetMidY(rect));
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:_layer.position];//起始点
        [path addQuadCurveToPoint:endPoint controlPoint:CGPointMake(ScreenWidth/2, rect.origin.y-50)];//控制点和终点
        
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];//路径动画
        pathAnimation.path = path.CGPath;
        
        CABasicAnimation *rotateAnimate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotateAnimate.fromValue = [NSNumber numberWithFloat:0];
        rotateAnimate.toValue = [NSNumber numberWithFloat:12];
        rotateAnimate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        
        CAAnimationGroup *groups = [CAAnimationGroup animation];
        groups.animations = @[pathAnimation,rotateAnimate];
        groups.duration = 1.2f;
        groups.removedOnCompletion=NO;
        groups.fillMode=kCAFillModeForwards;
        groups.delegate = self;
        [_layer addAnimation:groups forKey:@"group"];
        
        if (completion) {
            _animationfinish = completion;
        }
    }
    -(void)shakeAnimation:(UIView *)shakeView{
        CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        base.duration = 0.25f;
        base.autoreverses = YES;
        base.fromValue = [NSNumber numberWithFloat:-5];
        base.toValue = [NSNumber numberWithFloat:5];
        [shakeView.layer addAnimation:base forKey:nil];
        
    }
    
    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
        if (anim == [_layer animationForKey:@"group"]) {
            [_layer removeFromSuperlayer];
            _layer = nil;
            if (_animationfinish) {
                _animationfinish(YES);
            }
        }
    }
    @end
    

    demo地址 https://github.com/Butteryflyyer/AnimateGroup

    ios自习室欢迎进入,一起学习一起进步。

    IMG_7291.JPG

    相关文章

      网友评论

        本文标题:『ios』动画篇-购物车动画

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