IOS QuartzCore核心动画框架
- 使用核心动画需要引入的框架:#import
- CALayer: CoreAnimation
- CALayer就是UIView上的图层,很多的CALayer组成了UIView。
- CALayer与UIView是两个完全不同的类
- UIView动画就是简单动画,可以实现改变,偏移,形变,翻转,旋转,翻页
- QuartzCore.framework:
- CAAnimation:
- CAAnimationGroup: 并行动画
- CATransition:
- CAPropertyAnimation: 属性动画
- CABasicAnimation:
- CAKeyframeAnimation:
-
// 使用关键帧动画进行旋转
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
CATransform3D transform3d1 = CATransform3DMakeRotation(M_PI * 0.0, 0, 0, 1.0f);
CATransform3D transform3d2 = CATransform3DMakeRotation(M_PI * 0.5, 0, 0, 1.0f);
CATransform3D transform3d3 = CATransform3DMakeRotation(M_PI * 1.0, 0, 0, 1.0f);
CATransform3D transform3d4 = CATransform3DMakeRotation(M_PI * 1.5, 0, 0, 1.0f);
CATransform3D transform3d5 = CATransform3DMakeRotation(M_PI * 2.0, 0, 0, 1.0f);
NSValue *value1 = [NSValue valueWithCATransform3D:transform3d1];
NSValue *value2 = [NSValue valueWithCATransform3D:transform3d2];
NSValue *value3 = [NSValue valueWithCATransform3D:transform3d3];
NSValue *value4 = [NSValue valueWithCATransform3D:transform3d4];
NSValue *value5 = [NSValue valueWithCATransform3D:transform3d5];
keyFrame.values = @[value1, value2, value3, value4, value5];
keyFrame.duration = 20;
keyFrame.repeatCount = 100000;
[image.layer addAnimation:keyFrame forKey:nil];
-
// 声明一个动画
CATransition *animation = [CATransition animation];
// 设置动画时间
animation.duration = 1.0f;
// 设置动画动画的执行效果 慢--快--慢的效果
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 设置动画的样式
// animation.type = kCATransitionMoveIn;
animation.type = @"cube"; // 私有API中的立方体滚动
// 设置动画的移入的位置 从左、从右、从上、从下
// animation.subtype = kCATransitionFromTop;
animation.subtype = @"toLeft"; // 私有API中的从左面滚动
// 给animatedView添加动画
[animatedView.layer addAnimation:animation forKey:nil];
-
// 旋转
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI); // 只能旋转一次
CGAffineTransform transform2 = CGAffineTransformRotate(animatedView.transform, M_PI_4); // 可以多次旋转
// 缩小,放大
CGAffineTransform transform3 = CGAffineTransformMakeScale(1.2f, 1.2f); // 缩放一次
CGAffineTransform transform4 = CGAffineTransformScale(animatedView.transform, 0.9f, 0.9f); // 可以连续操作
// 移动
CGAffineTransform transform5 = CGAffineTransformMakeTranslation(12, 12);
CGAffineTransform transform6 = CGAffineTransformTranslate(animatedView.transform, 0, -120);
// 开始动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
[animatedView setTransform:transform6];
[UIView commitAnimations];
-
// CABasicAnimation
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D transform3d1 = CATransform3DMakeTranslation(10, 10, 10); // 3D移动效果
CATransform3D transform3d11 = CATransform3DTranslate(transform3d1, 10, 10, 10);
CATransform3D transform3d2 = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0); // 3D旋转效果
CATransform3D transform3d22 = CATransform3DRotate(transform3d2, M_PI, 0.0, 0.0, 1.0);
CATransform3D transform3d3 = CATransform3DMakeScale(0.2f, 1.2f, 1.2f); // 3D缩放效果
CATransform3D transform3d33 = CATransform3DTranslate(transform3d2, 0.0, 0.0, 0.0);
basicAnimation.toValue = [NSValue valueWithCATransform3D:transform3d1];
basicAnimation.removedOnCompletion = YES; // 动画结束后去除动画
basicAnimation.duration = 0.5f; // 设置动画时间
basicAnimation.cumulative = NO;
basicAnimation.repeatCount = 0; // 循环次数
[animatedView.layer addAnimation:basicAnimation forKey:nil];
-
// 只修改坐标的X轴
CABasicAnimation *basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
basicAnimation2.duration = 0.5f;
basicAnimation2.toValue = [NSNumber numberWithFloat:130];
[animatedView.layer addAnimation:basicAnimation2 forKey:nil];
// 修改position
CABasicAnimation *basicAnimation3 = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation3.duration = 0.5f;
basicAnimation3.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
[animatedView.layer addAnimation:basicAnimation3 forKey:nil];
// 修改透明度
CABasicAnimation *basicAnimation4 = [CABasicAnimation animationWithKeyPath:@"opacity"];
basicAnimation4.duration = 0.5f;
// 设置透明度的变化
basicAnimation4.toValue = [NSNumber numberWithFloat:0.0f];
// 添加上动画
[animatedView.layer addAnimation:basicAnimation4 forKey:nil];
-
// 动画组
// 初始化一个动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
// 动画组中的第一个动画
CABasicAnimation *basicAnimation5 = [CABasicAnimation animationWithKeyPath:@"opacity"];
basicAnimation5.duration = 0.5f;
basicAnimation5.toValue = [NSNumber numberWithFloat:0.0f];
// 动画组中的第二个动画
CABasicAnimation *basicAnimation6 = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation6.duration = 0.5f;
basicAnimation6.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
animationGroup.animations = @[basicAnimation5, basicAnimation6];
// 设置动画组的时间
animationGroup.duration = 0.5;
// 添加上
[animatedView.layer addAnimation:animationGroup forKey:nil];
-
CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// 运动路径的几个点
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(320/2, 0)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(320, 548/2)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(320/2, 548)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(0, 548/2)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(320/2, 0)];
// 移动到的四个点
keyframeAnimation.values = @[value1, value2, value3, value4, value5, value4, value3, value2, value1];
// 设置速度
keyframeAnimation.duration = 1.0f;
// 设置移动的形式
keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
keyframeAnimation.repeatCount = 10000000.0f;
// 添加上
[animatedView.layer addAnimation:keyframeAnimation forKey:nil];