仿射变换(CGAffineTransform)使用小结
一、理论基础
二、效果演示
三、实现代码
3.1:位移变换
-(void)positionAnimation{
_demoView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
_demoView.transform = CGAffineTransformMakeTranslation(100, 100);
}];
}
3.2:缩放变换
-(void)scaleAnimation{
_demoView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
_demoView.transform = CGAffineTransformMakeScale(2, 2);
}];
}
3.3:旋转变换
-(void)rotateAnimation{
_demoView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
_demoView.transform = CGAffineTransformMakeRotation(M_PI);
}];
}
3.4:组合变换
-(void)combinationAnimation{
//仿射变换的组合使用
_demoView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI);
CGAffineTransform transform2 = CGAffineTransformScale(transform1, 0.5, 0.5);
_demoView.transform = CGAffineTransformTranslate(transform2, 100, 100);
}];
}
3.5:矩阵的反转
-(void)invertAnimation{
_demoView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
//矩阵反转
_demoView.transform = CGAffineTransformInvert(CGAffineTransformMakeScale(2, 2));
}];
}
四、补充
- CGAffineTransformIdentity : 单位矩阵变换,一般用于仿射变换的初始化或者还原。
-
CGAffineTransformEqualToTransform(CGAffineTransform t1,
CGAffineTransform t2) : 判断两个变换矩阵是否相等 -
CGAffineTransformConcat(CGAffineTransform t1,
CGAffineTransform t2) : 得到两个矩阵相加后得到的最终矩阵 -
CGPoint CGPointApplyAffineTransform(CGPoint point,
CGAffineTransform t) : 某点通过矩阵变换之后的点 -
CGSize CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t) : 某个size通过矩阵变换之后的size
- CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t) : 某个Rect通过矩阵变换之后的区域