加入購物車的粒子動畫

lr0256 8年前發布 | 6K 次閱讀 iOS開發 移動開發

調用方式為直接調用CALayer的靜態方法

[CALayer startAnimatAtBeginView:btn endView:weakself.cartView finished:nil];

/**
 *  加入購物車的粒子動畫
 *
 *  @param begiView 開始地方的View
 *  @param endView  結束地方的View
 *  @param finished 動畫結束回調
 */
+ (void)startAnimatAtBeginView:(UIView *)begiView
                       endView:(UIView *)endView
                      finished:(void(^)(void))finished;

內部實現

+ (void)startAnimatAtBeginView:(UIView *)begiView endView:(UIView *)endView finished:(void(^)(void))finished
{

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    //獲取動畫起始點和結束點
    CGPoint startPoint = [begiView convertRect: begiView.bounds toView:window].origin;
    CGPoint endPoint = [endView convertRect: endView.bounds toView:window].origin;
    endPoint.x = endPoint.x + endView.bounds.size.width*0.5;
    endPoint.y = endPoint.y + endView.bounds.size.height*0.5;

    //設置粒子
    CALayer *dotLayer = [CALayer layer];
    dotLayer.backgroundColor = [UIColor redColor].CGColor;
    dotLayer.frame = CGRectMake(0, 0, 15, 15);
    dotLayer.cornerRadius = 15 /2;
    [window.layer addSublayer:dotLayer];

    dotLayer.endView = endView;
    dotLayer.finished = finished;

    //設置貝塞爾曲線
    UIBezierPath *path= [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    //三點曲線,設置路徑
    [path addCurveToPoint:endPoint
            controlPoint1:startPoint
            controlPoint2:CGPointMake(startPoint.x - 180, startPoint.y - 150)];


    //設置動畫組
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;

    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = 0.1f;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = 0.5f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = dotLayer;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [dotLayer addAnimation:groups forKey:nil];
}

粒子動畫結束

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
        !self.finished? :self.finished();

        CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        shakeAnimation.duration = 0.1f;
        shakeAnimation.fromValue = [NSNumber numberWithFloat:0.7];
        shakeAnimation.toValue = [NSNumber numberWithFloat:1];
        shakeAnimation.autoreverses = YES;
        [self.endView.layer addAnimation:shakeAnimation forKey:nil];

        [self removeFromSuperlayer];
    }
}

 

來自:http://www.jianshu.com/p/a726ae8526fc

 

 本文由用戶 lr0256 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!