CoreGraphics框架中的CATransform3D类可用于设定CALayer及其子类的transform属性,控制layer的缩放、旋转和平移等操作。
1、定义:
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;
2、原理:
一个任意的3D变换都能表示为 乘以一个4 x 4矩阵 ,即经历一个4x4的矩阵的变换。
3、解读:
当一个点(x, y, z)经过3D变换后变为另外一个点(x1, y1, z1)的过程可以表示为:
(1、)常用方法与定义:
/* The identity transform: [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]. */
// 单位矩阵
CA_EXTERN const CATransform3D CATransform3DIdentity
// 判断矩阵是否为3D单位矩阵
CA_EXTERN bool CATransform3DIsIdentity (CATransform3D t)
// 判断两个3D矩阵是否一样
CA_EXTERN bool CATransform3DEqualToTransform (CATransform3D a, CATransform3D b)
/* Returns a transform that translates by '(tx, ty, tz)':
* t' = [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */
// 平移
CA_EXTERN CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz)
/* Returns a transform that scales by `(sx, sy, sz)':
* t' = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. */
// 缩放
CA_EXTERN CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz)
/* Returns a transform that rotates by 'angle' radians about the vector
* '(x, y, z)'. If the vector has length zero the identity transform is returned. */
// 旋转
CA_EXTERN CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
#prama -----------以上的矩阵变换均是针对默认单位矩阵做出的变换!!!
#prama -----------即单独使用别管你调用了那个,调用了几次,最后结果仅仅是针对单位矩阵做了最后一步的变换!!!
/* Translate 't' by '(tx, ty, tz)' and return the result: t' = translate(tx, ty, tz) * t. */
// 在某一3D变换的基础上平移
CA_EXTERN CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz)
/* Scale 't' by '(sx, sy, sz)' and return the result: t' = scale(sx, sy, sz) * t. */
// 在某一3D变换的基础上缩放
CA_EXTERN CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx, CGFloat sy, CGFloat sz)
/* Rotate 't' by 'angle' radians about the vector '(x, y, z)' and return the result.
* If the vector has zero length the behavior is undefined: t' = rotation(angle, x, y, z) * t. */
// 在某一3D变换的基础上旋转
CA_EXTERN CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
/* Concatenate 'b' to 'a' and return the result: t' = a * b.*/
// 组合两个3D变换
CA_EXTERN CATransform3D CATransform3DConcat (CATransform3D a, CATransform3D b)
#prama -----------以上涉及矩阵变换的矩阵可以将多重效果组合使用!
/* Invert 't' and return the result. Returns the original matrix if 't' has no inverse. */
// 对某一3D变换结果取反
CA_EXTERN CATransform3D CATransform3DInvert (CATransform3D t)
/* Return a transform with the same effect as affine transform 'm'. */
// 实现与仿射变换函数效果相同的3D变换,即仿射变换转换成3D变换
CA_EXTERN CATransform3D CATransform3DMakeAffineTransform (CGAffineTransform m)
/* Returns true if 't' can be represented exactly by an affine transform. */
// 判断该3D变换是否可以通过仿射变换精确地表示。因为所有仿射变换都可以用3D变换来表示,但是反之则不行。
CA_EXTERN bool CATransform3DIsAffine (CATransform3D t)
/* Returns the affine transform represented by 't'. If 't' can not be
* represented exactly by an affine transform the returned value is undefined. */
// 返回由't'表示的仿射变换。 如果't'不能通过仿射变换精确地表示,则返回的值是未定义的
CA_EXTERN CGAffineTransform CATransform3DGetAffineTransform (CATransform3D t)
@interface NSValue (CATransform3DAdditions)
// NSValue与CATransform3D相互转换的接口
+ (NSValue *)valueWithCATransform3D:(CATransform3D)t;
@property(readonly) CATransform3D CATransform3DValue;
@end
示例一、
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_blueLayer = [CALayer layer];
[_blueLayer setBackgroundColor:[UIColor blueColor].CGColor];
[_blueLayer setFrame:CGRectMake(50, 100, 100, 100)];
[self.view.layer addSublayer:_blueLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (once) {
[UIView animateWithDuration:2.0 animations:^{
_blueLayer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
}];
once = NO;
}else{
[UIView animateWithDuration:2.0 animations:^{
_blueLayer.transform = CATransform3DMakeTranslation(100, 0, 0);
}];
once = YES;
}
}
示例二、
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_blueLayer = [CALayer layer];
[_blueLayer setBackgroundColor:[UIColor blueColor].CGColor];
[_blueLayer setContents:(id)[[UIImage imageNamed:@"lovesick"] CGImage]];
[_blueLayer setFrame:CGRectMake(50, 100, 200, 350)];
[self.view.layer addSublayer:_blueLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:10.0 animations:^{
CGAffineTransform affineTransform_1 = CGAffineTransformMakeTranslation(200, 0);
CGAffineTransform affineTransform_2 = CGAffineTransformMakeScale(0.5, 0.5);
CGAffineTransform affineTransform_all = CGAffineTransformConcat(affineTransform_1, affineTransform_2);
CATransform3D transform3D_1 = CATransform3DMakeAffineTransform(affineTransform_all);
CATransform3D transform3D_2 = CATransform3DRotate(transform3D_1, M_PI/2, 0, 0.5, 0.5);
_blueLayer.transform = transform3D_2;
}];
}
(2、)其他方法
- 除了上述系统的函数外还可以直接修改结构体参数来实现3D变换。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:10.0 animations:^{
CATransform3D transform3D = CATransform3DIdentity;
transform3D.m41 = 200;
_blueLayer.transform = transform3D;
}];
}
普通转换的矩阵配置影响参数:
在这里特别需要注意的是参数m34, 这一参数主要涉及到设置透视效果,但是必须要有旋转角度才能显示出来,否则无效果。