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];
}
}