对于仿射变换的理解,以本人现阶段水平还谈不上是深入了解,我喜欢把它想象成一种简单的动画效果,下面介绍几种简单的变化:位置移动,按一定比例缩放,顺时针、逆时针旋转
#import "ViewController.h"
@interface
ViewController ()
@property (nonatomic,weak)
UIView * rectView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
//"点我"
控制按钮
UIButton * button=[UIButton
buttonWithType:UIButtonTypeCustom];
[button setTitle:@"点我"
forState:UIControlStateNormal];
button.frame=CGRectMake(self.view.frame.size.width-110,
20, 100,
44);
button.backgroundColor=[UIColor
redColor];
//添加点击事件
[button addTarget:self
action:@selector(clickMeAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
UIView * rectView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, 150,
150)];
rectView.center=self.view.center;
rectView.backgroundColor=[UIColor
greenColor];
self.rectView=rectView;
[self.view
addSubview:rectView];
}
#pragma mark - clickAction
- (void) clickMeAction
{
//仿射变换移动
//1.从当前位置,向右移动50,向下移动100 (直接变换)
/*
self.rectView.transform=CGAffineTransformMakeTranslation(50, 100);
*/
//2.(方法一)从当前位置,向右移动50,向下移动100
(0.5秒钟时延)
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeTranslation(50, 100);
}];
*/
//3.(方法二)从当前位置,向右移动50,向下移动100
(0.5秒钟时延)
/*
[UIView animateWithDuration:0.5 animations:^{
//self.rectView.transform=CGAffineTransformMakeTranslation(50, 100);
self.rectView.transform=CGAffineTransformTranslate(self.rectView.transform, 100, 100);
}];
*/
//仿射变换比例
//1.中心点不变,宽度缩小为原来的0.1倍,高度缩短为原来的0.5倍
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeScale(0.1, 0.5);
}];
*/
//2.中心点不变,扩大为原来的5倍
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeScale(5, 5);
}];
*/
//3.(方法一)向右向下各移动100
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeTranslation(100, 100);
}];
*/
//4.(方法二)向右向下各移动100
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeTranslation(100, 100);
}];
*/
//5.在前一个位置的基础之上,向右向下分别移动1个距离
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformTranslate(self.rectView.transform,1,1);
}];
*/
//6.在前一个位置的基础之上,向右向下分别移动100个距离
/*
[UIView animateWithDuration:0.5 animations:^{
CGAffineTransform form=self.rectView.transform;
self.rectView.transform=CGAffineTransformTranslate(form,100,100);
}];
*/
//7.来回弹跳切换着向右下角移动10个单位
/*
[UIView animateWithDuration:0.5 animations:^{
CGAffineTransform form=self.rectView.transform;
self.rectView.transform=CGAffineTransformMakeScale(2, 2);
self.rectView.transform=CGAffineTransformTranslate(form,10,10);
}];
*/
//仿射变换---旋转
//1.顺时针旋转90度
/*
[UIView animateWithDuration:0.5 animations:^{
self.rectView.transform=CGAffineTransformMakeRotation(M_PI_2);
}];
*/
//2.在前一个位置的基础之上,顺时针旋转45度
/*
[UIView animateWithDuration:0.5 animations:^{
CGAffineTransform form=self.rectView.transform;
self.rectView.transform=CGAffineTransformRotate(form,M_PI_4);
}];
*/
//3.在前一个位置的基础之上,逆时针旋转45度
/*
[UIView animateWithDuration:0.5 animations:^{
CGAffineTransform form=self.rectView.transform;
self.rectView.transform=CGAffineTransformRotate(form,-M_PI_4);
}];
*/
//4.在前一个位置的基础之上,顺时针旋转确定的度数
[UIView
animateWithDuration:0.5
animations:^{
CGAffineTransform form=self.rectView.transform;
self.rectView.transform=CGAffineTransformRotate(form,9/100.0*M_PI);
}];
}
@end