- (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(100, 100, 50, 50)];
rectView.backgroundColor = [UIColor greenColor];
self.rectView = rectView;
[self.view addSubview:rectView];
}
- (void)clickMeAction
{
// 1、仿射变换移动
// (block动画从6.几开始出现)
[UIView animateWithDuration:1 animations:^{
// 方法一:相对于最初的位置
// self.rectView.transform = CGAffineTransformMakeTranslation(50, 100);
// 方法二:相对于变化后的位置:self.rectView.transform
// self.rectView.transform = CGAffineTransformTranslate(self.rectView.transform, 50, 100);
}];
// 2、仿射变换比例
[UIView animateWithDuration:1 animations:^{
// 方法一
// self.rectView.transform = CGAffineTransformMakeScale(2, 2);//中心点不变
// 方法二
// self.rectView.transform = CGAffineTransformScale(self.rectView.transform, 2, 2);
}];
// 3、仿射变换旋转
[UIView animateWithDuration:1 animations:^{
// 方法一
// self.rectView.transform = CGAffineTransformMakeRotation(M_PI_4);
// 方法二
// self.rectView.transform = CGAffineTransformRotate(self.rectView.transform, -M_PI_4);
// 度数变弧度
// 49/180.0*M_PI
}];
// 4、三种变换的结合
// (1)Translation,Scale(scale变换的时候,会影响Translation的变化,所以移动的变化使用frame或者center)
// [UIView animateWithDuration:1 animations:^{
// self.rectView.transform = CGAffineTransformMakeScale(2, 2);
// CGRect frame = self.rectView.frame;
// frame.origin.x += 50;
// frame.origin.y += 50;
// self.rectView.frame = frame;
// }];
// 或者,动画分开
// [UIView animateWithDuration:1 animations:^{
// self.rectView.transform = CGAffineTransformMakeScale(2, 2);
// }];
//
// [UIView animateWithDuration:1 animations:^{
// CGRect frame = self.rectView.frame;
// frame.origin.x += 50;
// frame.origin.y += 50;
// self.rectView.frame = frame;
// }];
// (2)Translation,Rotate(rotate的变换会影响 锚点 的位置(锚点不会随着旋转而变化),所以移动使用center。使用frame和transform 都有问题)
// [UIView animateWithDuration:1 animations:^{
// CGPoint point = self.rectView.center;
// point.x += 50;
// point.y += 50;
// self.rectView.center = point;
// self.rectView.transform = CGAffineTransformRotate(self.rectView.transform, -M_PI_4);
// }];
// (3)Scale,Rotate(完美搭配)
[UIView animateWithDuration:1 animations:^{
self.rectView.transform = CGAffineTransformScale(self.rectView.transform, 2, 2);
self.rectView.transform = CGAffineTransformRotate(self.rectView.transform, -M_PI_4);
}];