关键帧动画
2016-07-08 本文已影响34人
我的梦想之路
CALayer *transitionLayer = [[CALayer alloc] init];
//开启一个动画事务
[CATransaction begin];
//CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务.kCATransactionDisableActions
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
transitionLayer.opacity = 0.6;
//contents是layer的一个属性
UIImageView *tempimgview;
for (UIView *aView in [picView subviews]) // picView代表需要执行动画的视图
{
if([aView isKindOfClass:[UIImageView class]]){
//NSLog(@"=====找到imageview");
tempimgview = (UIImageView*)aView;
break;
}
}
//设置CALayer的内容
transitionLayer.contents = (id)tempimgview.layer.contents;
//父类不同,所以需要坐标系统的转换,必须是处于同一window内
transitionLayer.frame = [[UIApplication sharedApplication].keyWindow convertRect:tempimgview.bounds fromView:tempimgview];
[[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer];
[UIView beginAnimations:@"imageViewAnimation" context:(__bridge void *)(tempimgview)];
/// 提交事务
[CATransaction commit];
//路径曲线:贝塞尔曲线,使动画按照你所设定的贝塞尔曲线运动
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:transitionLayer.position];
//传入购物车的坐标(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)];
//关键帧
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)];
//缩小动画结束移除
scaleAnim.removedOnCompletion = YES;
NSLog(@"=====in enjoy55555");
//将抛物动画和缩小动画加入动画组,可以执行多个动画,并且设置动画的执行时间
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"];
静止界面
动画开始
动画执行
动画执行