天天看點

模仿餓了麼、美團添加購物車的動畫效果

#pragma mark -加入購物車動畫

-(void) JoinCartAnimationWithRect:(CGRect)rect

{

    CGFloat _endPoint_x = 35;

    CGFloat _endPoint_y = 713 - 35;

    CGFloat startX = rect.origin.x;

    CGFloat startY = rect.origin.y;

    //1.建立佩塞爾曲線:

    _path= [UIBezierPath bezierPath];

    //設定起點:

    [_path moveToPoint:CGPointMake(startX, startY)];

    //建立路徑:三點曲線

    [_path addCurveToPoint:CGPointMake(_endPoint_x, _endPoint_y) controlPoint1:CGPointMake(startX, startY) controlPoint2:CGPointMake(startX - 180, startY - 200)];

    //2.建立layer圖示:

    _dotLayer = [CALayer layer];

    _dotLayer.backgroundColor = [UIColor purpleColor].CGColor;

    _dotLayer.frame = CGRectMake(0, 0, 20, 20);

    _dotLayer.cornerRadius = 5;

    _dotLayer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"01.png"]].CGColor;

    [self.view.layer addSublayer:_dotLayer];

    //3、組合動畫:

    [self groupAnimation];

}

#pragma mark - 組合動畫

-(void)groupAnimation

{

    //1、

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

    animation.path = _path.CGPath;

    animation.rotationMode = kCAAnimationRotateAuto;

    //2、

    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];

    alphaAnimation.duration = 0.5f;

    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];

    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];

    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    //3、

    CAAnimationGroup *groups = [CAAnimationGroup animation];

    groups.animations = @[animation,alphaAnimation];

    groups.duration = durationTime;

    groups.removedOnCompletion = NO;

    groups.fillMode = kCAFillModeForwards;

    groups.delegate = self;

    [groups setValue:@"groupsAnimation" forKey:@"animationName"];

    [_dotLayer addAnimation:groups forKey:nil];

    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:durationTime];

}

//執行完後移除:

- (void)removeFromLayer:(CALayer *)layerAnimation{

    [layerAnimation removeFromSuperlayer];

}

#pragma mark - CAAnimationDelegate

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

{

    if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {

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

        shakeAnimation.duration = 0.25f;

        shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];

        shakeAnimation.toValue = [NSNumber numberWithFloat:1];

        shakeAnimation.autoreverses = YES;

        // 這裡是下方的自定義View上面 放的btn. 自己随便定義一個 0.-

        //[_shopCartView.btnBackImg.layer addAnimation:shakeAnimation forKey:nil];

}

}

版權聲明:本文為CSDN部落客「weixin_34406086」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_34406086/article/details/92438950