加入購物車的粒子動(dòng)畫

Demo 下載

111111.gif

調(diào)用方式為直接調(diào)用CALayer的靜態(tài)方法

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

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

內(nèi)部實(shí)現(xiàn)

+ (void)startAnimatAtBeginView:(UIView *)begiView endView:(UIView *)endView finished:(void(^)(void))finished
{
   
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //獲取動(dòng)畫起始點(diǎn)和結(jié)束點(diǎn)
    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;

    //設(shè)置粒子
    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;
    
    //設(shè)置貝塞爾曲線
    UIBezierPath *path= [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    //三點(diǎn)曲線,設(shè)置路徑
    [path addCurveToPoint:endPoint
            controlPoint1:startPoint
            controlPoint2:CGPointMake(startPoint.x - 180, startPoint.y - 150)];
    
    
    //設(shè)置動(dòng)畫組
    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];
}

粒子動(dòng)畫結(jié)束

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
        !self.finished? :self.finished();
    
// 設(shè)置購物車的抖動(dòng)動(dòng)畫
        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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Core Animation Core Animation,中文翻譯為核心動(dòng)畫,它是一組非常強(qiáng)大的動(dòng)畫處理API,...
    45b645c5912e閱讀 3,155評論 0 21
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,690評論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,268評論 5 13
  • 1.自定義控件 a.繼承某個(gè)控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,698評論 2 4
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場動(dòng)畫。 1.UIView...
    請叫我周小帥閱讀 3,318評論 1 23

友情鏈接更多精彩內(nèi)容