CALayer *transitionLayer = [[CALayer alloc] init];
//開啟一個動畫事務(wù)
[CATransaction begin];
//CATransaction 事務(wù)類,可以對多個layer的屬性同時進(jìn)行修改.它分隱式事務(wù),和顯式事務(wù).kCATransactionDisableActions
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
transitionLayer.opacity = 0.6;
//contents是layer的一個屬性
UIImageView *tempimgview;
for (UIView *aView in [picView subviews]) // picView代表需要執(zhí)行動畫的視圖
{
if([aView isKindOfClass:[UIImageView class]]){
//NSLog(@"=====找到imageview");
tempimgview = (UIImageView*)aView;
break;
}
}
//設(shè)置CALayer的內(nèi)容
transitionLayer.contents = (id)tempimgview.layer.contents;
//父類不同,所以需要坐標(biāo)系統(tǒng)的轉(zhuǎn)換峻黍,必須是處于同一window內(nèi)
transitionLayer.frame = [[UIApplication sharedApplication].keyWindow convertRect:tempimgview.bounds fromView:tempimgview];
[[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer];
[UIView beginAnimations:@"imageViewAnimation" context:(__bridge void *)(tempimgview)];
/// 提交事務(wù)
[CATransaction commit];
//路徑曲線:貝塞爾曲線钠绍,使動畫按照你所設(shè)定的貝塞爾曲線運(yùn)動
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:transitionLayer.position];
//傳入購物車的坐標(biāo)(X,Y)
CGPoint toPoint = CGPointMake(self.view.frame.size.width-20, 40);
//
[movePath addQuadCurveToPoint:toPoint
controlPoint:CGPointMake(self.view.frame.size.width-50,transitionLayer.position.y-50)];
//關(guān)鍵幀
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.path = movePath.CGPath;
positionAnimation.removedOnCompletion = YES;
//縮小動畫
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
//縮小動畫結(jié)束移除
scaleAnim.removedOnCompletion = YES;
NSLog(@"=====in enjoy55555");
//將拋物動畫和縮小動畫加入動畫組,可以執(zhí)行多個動畫谜酒,并且設(shè)置動畫的執(zhí)行時間
CAAnimationGroup *group = [CAAnimationGroup animation];
group.beginTime = CACurrentMediaTime();
group.duration = 0.7;
group.animations = [NSArray arrayWithObjects:positionAnimation,scaleAnim,nil];
group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = YES;
group.autoreverses= NO;
group.delegate=self;
[transitionLayer addAnimation:group forKey:@"opacity"];
靜止界面
動畫開始
動畫執(zhí)行
動畫執(zhí)行