图层变换 - 二维变换(仿射变换)(混合变换)(斜切变换)
这里 我们先认识一个宏
//度数转换
#define DEGREES_TO_RADIANS(x) ((x)/180.0*M_PI) //转换为多少度
仿射变换:
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(15.0));
CGAffineTransform transform = CGAffineTransformMakeScale(1, 1); //设置拉伸或缩放(x为横向拉伸倍数,y为竖向拉伸倍数)
CGAffineTransform transform = CGAffineTransformMakeTranslation(100, 200); //设置偏移量(x为横向偏移数(正值为右),y为竖向偏移数(正值为下))
_showView.layer.affineTransform = transform; //添加仿射变换
斜切变换:
CGAffineTransform transform = CGAffineTransformIdentity;//创建一个斜切变换(-c为横向斜度,b为竖向斜度)
transform.c = -1;
transform.b = 0;
_showView.layer.affineTransform = transform; //将创建的变换添加到图层中
混合变换:
- 方法一:将已有变换添加变换属性生成新的变换
CGAffineTransform transform = CGAffineTransformIdentity; //创建一个空的变换容器
transform = CGAffineTransformScale(transform, 0.5, 0.5); //缩放
transform = CGAffineTransformRotate(transform, M_PI / 180.0 * 30.0); //旋转
transform = CGAffineTransformTranslate(transform, 200, 0); //位移
_showView.layer.affineTransform = transform; //变换添加到图层中
- 方法二:将两个变换合在一起
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(1.0)); //设置旋转
CGAffineTransform transform2 = CGAffineTransformMakeScale(1, 1); //设置拉伸或缩放(x为横向拉伸倍数,y为竖向拉伸倍数)
CGAffineTransform syntheticTransform = CGAffineTransformConcat(transform, transform2); //将两个变换容器合成一个变换容器
_showView.layer.affineTransform = syntheticTransform; //添加仿射变换