1. 首先明白CATransform3D是个什么东西,是一个四维变换矩阵,描述3d空间
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
m11:x轴方向进行缩放
m12:和m21一起决定z轴的旋转
m13:和m31一起决定y轴的旋转
m14:
m21:和m12一起决定z轴的旋转
m22:y轴方向进行缩放
m23:和m32一起决定x轴的旋转
m24:
m31:和m13一起决定y轴的旋转
m32:和m23一起决定x轴的旋转
m33:z轴方向进行缩放
m34:透视效果m34= -1/D,D越小,透视效果越明显,必须在有旋转效果的前提下,才会看到透视效果
m41:x轴方向进行平移
m42:y轴方向进行平移
m43:z轴方向进行平移
m44:初始为1
2.一些常用的方法
//初始化一个transform3D对象,不做任何变换
const CATransform3D CATransform3DIdentity;
//判断一个transform3D对象是否是初始化的对象
bool CATransform3DIsIdentity (CATransform3D t);
//比较两个transform3D对象是否相同
bool CATransform3DEqualToTransform (CATransform3D a, CATransform3D b);
//将两个 transform3D对象变换属性进行叠加,返回一个新的transform3D对象
CATransform3D CATransform3DConcat (CATransform3D a, CATransform3D b);
- 不带make和带make的区别是,没有Make的是叠加原理
带make的可以使用 CATransform3DConcat (CATransform3D a, CATransform3D b);达到和不带Make的一样的效果
//返回一个平移变换的transform3D对象 tx,ty,tz对应x,y,z轴的平移
CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz);
//在某个transform3D变换的基础上进行平移变换,t是上一个transform3D,其他参数同上
CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz);
//缩放
CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy,
CGFloat sz)
CATransform3D CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx,
CGFloat sy, CGFloat sz)
//旋转
CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x,
CGFloat y, CGFloat z)
CATransform3DRotate (CATransform3D t, CGFloat angle,
CGFloat x, CGFloat y, CGFloat z)
//反转't'并返回结果。 如果't'没有逆,返回原始矩阵。
CATransform3D CATransform3DInvert (CATransform3D t)
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D t0 = CATransform3DIdentity;
NSValue *value1 = [NSValue valueWithCATransform3D:t0];
animation.fromValue = value1;
CATransform3D t1 = CATransform3DMakeScale(0.5, 0.5, 1.0);
NSValue *value2 = [NSValue valueWithCATransform3D:t1];
animation.toValue = value2;
animation.duration = 2.0;
animation.autoreverses = YES;
animation.repeatCount = 2;
animation.removedOnCompletion = NO;
//不加这句话,执行完动画会回到原来
self.imageView.layer.transform = t1;
[self.imageView.layer addAnimation:animation forKey:nil];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D t0 = CATransform3DIdentity;
NSValue *value1 = [NSValue valueWithCATransform3D:t0];
animation.fromValue = value1;
//不带make的效果叠加
CATransform3D t1 = CATransform3DScale(t0, 0.5, 0.5, 1.0);
t1 = CATransform3DTranslate(t1, 100, 100, 0);
t1 = CATransform3DRotate(t1, M_PI_4, 0, 1, 0);
NSValue *value2 = [NSValue valueWithCATransform3D:t1];
animation.toValue = value2;
animation.duration = 0.5;
animation.autoreverses = NO;
animation.removedOnCompletion = NO;
//不加这句话,执行完动画会回到原来
self.imageView.layer.transform = t1;
[self.imageView.layer addAnimation:animation forKey:nil];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D t0 = CATransform3DIdentity;
NSValue *value1 = [NSValue valueWithCATransform3D:t0];
animation.fromValue = value1;
//带make的叠加
CATransform3D t1 = CATransform3DMakeScale( 0.5, 0.5, 1.0);
CATransform3D t2 = CATransform3DMakeTranslation(100, 100, 0);
CATransform3D t3 = CATransform3DMakeRotation(M_PI_4, 0, 1, 0);
CATransform3D t4 = CATransform3DConcat(t1, t2);
CATransform3D t5 = CATransform3DConcat(t4, t3);
NSValue *value2 = [NSValue valueWithCATransform3D:t5];
animation.toValue = value2;
animation.duration = 0.5;
animation.autoreverses = NO;
animation.removedOnCompletion = NO;
//不加这句话,执行完动画会回到原来
self.imageView.layer.transform = t5;
[self.imageView.layer addAnimation:animation forKey:nil];
- m34使用方法,透视效果m34= -1/D,D越小,透视效果越明显,必须在有旋转效果的前提下,才会看到透视效果
CATransform3D t3 = CATransform3DIdentity;
t3.m34 = -1/100.0;
t3 = CATransform3DRotate(t3,M_PI_4, 0, 1, 0);
//不加这句话,执行完动画会回到原来
self.imageView.layer.transform = t3;
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"屏幕快照 2015-12-06 下午3.27.15.png"];
imageView.layer.transform = CATransform3DMakeRotation(M_PI/4, 0, 0, 1);
[self.view addSubview:imageView];
UIImageView * newImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
newImageView.image=[UIImage imageNamed:@"屏幕快照 2015-12-06 下午3.27.15.png"];
[self.view addSubview:newImageView];
CATransform3D trans = CATransform3DMakeRotation(M_PI/4, 0, 0, 1);
trans = CATransform3DInvert(trans);
newImageView.layer.transform =trans;
3.CATransform和CGAffineTransform的区别,前者用于3d变换,作用在layer上,配合coreAnimation使用,后者是UIview的2d变化,作用在view上
//将一个CGAffinrTransform转化为CATransform3D
CATransform3D CATransform3DMakeAffineTransform (CGAffineTransform m);
//判断一个CATransform3D是否可以转换为CAAffineTransform
bool CATransform3DIsAffine (CATransform3D t);
//将CATransform3D转换为CGAffineTransform
CGAffineTransform CATransform3DGetAffineTransform (CATransform3D t);