【IOS】CGAffineTransform、CATransfo
1. 2D效果 CGAffineTransform ( view.transform )
transform自定义方法 有6个参数
CGAffineTransformMake ( CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty );
x : a*x0 + c*y0 + tx; (像素点宽)
y : b*x0 + d*y0 + ty; (像素点高)
原画
旋转;
x : cos(θ)*x0 - sin(θ)*y0;
y : sin(θ)*x0 + cos(θ)*y0;
//偏转加倾斜
CGFloatjd = -M_PI_4;
CGFloat scaleX = 0.5;
CGFloat scaleY = 1;
self.imageView.transform=CGAffineTransformMake(scaleX*cosf(jd),scaleY*sinf(jd),scaleX*-sinf(jd),scaleY*cosf(jd),0,0);
2. 3D效果 CATransform3D ( view.layer.transform )
structCATransform3D
{
CGFloatm11, m12, m13, m14;
CGFloatm21, m22, m23, m24;
CGFloatm31, m32, m33, m34;
CGFloatm41, m42, m43, m44;
};
我找了一张课件的截图
CATransform3D doctorTran = CATransform3DIdentity;
CGFloat jd = M_PI_4;
(1)旋转 rotate
绕Z轴
{
cos(θ) ,-sin(θ) ,0 ,0
sin(θ) , cos(θ) ,0 ,0
0 ,0 , 1 ,0
0 ,0 ,0 ,1
}
doctorTran.m11 = cosf(jd);
doctorTran.m12 = -sinf(jd);
doctorTran.m21 = sinf(jd);
doctorTran.m22 = cosf(jd);
绕Y轴
{
cos(θ) ,0 ,sin(θ) ,0
0 ,1 ,0 ,0
-sin(θ) ,0 ,cos(θ) ,0
0 ,0 ,0 ,1
}
doctorTran.m11=cosf(jd);
doctorTran.m13=sinf(jd);
doctorTran.m31= -sinf(jd);
doctorTran.m33=cosf(jd);
绕X轴
{
1 ,0 ,0 ,0
0 ,cos(θ) ,-sin(θ) ,0
0 ,sin(θ) ,cos(θ) ,0
0 ,0 , 0 ,1
}
doctorTran.m22=cosf(jd);
doctorTran.m23= -sinf(jd);
doctorTran.m32=sinf(jd);
doctorTran.m33=cosf(jd);
(2)切变 shear
沿X轴
{
1 ,k ,0 ,0
0 ,1 ,0 ,0
0 ,0 ,1 ,0
0 ,0 ,0 ,1
}
doctorTran.m12=1;
沿Y轴
{
1 ,0 ,0 ,0
k ,1 ,0 ,0
0 ,0 ,1 ,0
0 ,0 ,0 ,1
}
doctorTran.m21=1;
(3)镜像
基于Y-X平面
{
1 ,0 ,0 ,0
0 ,1 ,0 ,0
0 ,0 ,-1 ,0
0 ,0 ,0 ,1
}
基于X-Z平面
{
1 ,0 ,0 ,0
0 ,-1 ,0 ,0
0 ,0 ,1 ,0
0 ,0 ,0 ,1
}
基于Z-Y平面
{
-1 ,0 ,0 ,0
0 ,1 ,0 ,0
0 ,0 ,1 ,0
0 ,0 ,0 ,1
}
doctorTran.m11= -1;
(4)透视投影
m34 = -1/d;
d值决定了观察点的位置,d为正无穷大的时候,观察点在无穷远处,此时投影线垂直于投影平面,CATransform3D中m34的默认值为0,即观察点在无穷远处。m14,m24同理。
- (CATransform3D) get3DTransform {
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -2000;
return transform;
}
旋转Y轴时 锚点默认(0.5,0.5)
//doctorTran.m14 = 1.0/-300.0;//x
doctorTran.m24 = 1.0/-300.0;//y
//doctorTran.m34 = 1.0/-300.0;
doctorTran.m11=cosf(jd);
doctorTran.m13=sinf(jd);
doctorTran.m31= -sinf(jd);
doctorTran.m33=cosf(jd);
旋转Z轴时 让视角离得更近
//doctorTran.m14 = 1.0/-300.0;//x
doctorTran.m24=1.0/-100.0;//y
//doctorTran.m34 = 1.0/-300.0;
doctorTran.m11=cosf(jd);
doctorTran.m12= -sinf(jd);
doctorTran.m21=sinf(jd);
doctorTran.m22=cosf(jd);