设置transform的清空属性后组件会回到原始位置。
UIButton与UIImageView的区别:
代码验证,实例如下:
新建一个具有simple View的工程
编辑当前控制器的.h文件如下:
<span style="font-size:18px;">// // ViewController.h // 动态创建控件 // // Created by apple on 15/8/30. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (nonatomic, strong) UIButton *showButton; // 展示的按钮 @property (nonatomic, strong) UIButton *transButton; // 平移 @property(nonatomic, strong) UIButton *scaleButton; // 缩放 @property(nonatomic, strong) UIButton *rotalButton; // 旋转 @property(nonatomic,strong) UIButton *originButton; // 回到原来位置 @property(nonatomic, strong) UIButton *changeColorButton; // 改变当前View的背景色和 各个按钮的背景色 @property(nonatomic, strong ) UIButton *removeButton; // 移除 /* 添加各个按钮的监听方法: 完全可以为所有按钮添加一个参数为UIButton * 类型的参数的方法, 根据传进参数的tag值进行判断进而分别执行不同的相应操作 在此为了使代码更清晰,就分别为每一个按钮添加一个无参数的监听方法。 */ -(void) trans; // 此方法用于平移showButton的位置 -(void) scale; // 此方法用于改变showButton的大小 -(void) rotal; // 此方法用于旋转 -(void)origin; // 此方法用于把showButton归位到起始位置 -(void) change; // 此方法用于给当前View内的所有Button组件分别设置不同的背景颜色 -(void)remove; // 此方法用于移出当前View内的所有控件(除了自身控件) @end </span>编辑当前控制器的.m文件如下:
<span style="font-size:18px;">// // ViewController.m // 动态创建控件 // // Created by apple on 15/8/30. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "ViewController.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置要展示的控件按钮 self.showButton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH/4, WIDTH/6, WIDTH/2, WIDTH/2)]; // 为showButton组件设置正常状态下的各种属性 [ self.showButton setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal]; // 为按钮设置正常状态下的背景图片 [self.showButton setTitle:@"这是摩托" forState:UIControlStateNormal]; self.showButton.tag = 10; // 设置tag标志 [self.showButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 为showButton组件设置高亮状态下的各种属性 [self.showButton setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateHighlighted]; // 设置高亮状态下的背景图片 [self.showButton setTitle:@"这是汽车" forState:UIControlStateHighlighted]; [self.showButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; [self.view addSubview:self.showButton]; // 设置平移按钮的属性 self.transButton = [[UIButton alloc] initWithFrame:CGRectMake(10, WIDTH, (WIDTH-30)/2, 30)]; [self.transButton setTitle:@"平移" forState:UIControlStateNormal]; [self.transButton setTitle:@"触发平移" forState:UIControlStateHighlighted]; self.transButton.backgroundColor = [UIColor grayColor]; self.transButton.tag = 20; [self.transButton addTarget:self action:@selector(trans) forControlEvents:UIControlEventTouchUpInside]; // 添加监听方法 [self.view addSubview:self.transButton]; // 设置缩放按钮的属性 self.scaleButton = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH-30)/2+20, WIDTH, (WIDTH-30)/2, 30)]; [self.scaleButton setTitle:@"缩放" forState:UIControlStateNormal]; [self.scaleButton setTitle:@"触发缩放" forState:UIControlStateHighlighted]; self.scaleButton.backgroundColor = [UIColor grayColor]; self.scaleButton.tag = 30; [self.scaleButton addTarget:self action:@selector(scale) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.scaleButton]; // 设置旋转按钮的属性 self.rotalButton = [[UIButton alloc] initWithFrame:CGRectMake(10, WIDTH+30+10, (WIDTH-30)/2, 30)]; [self.rotalButton setTitle:@"旋转" forState:UIControlStateNormal]; [self.rotalButton setTitle:@"触发旋转" forState:UIControlStateHighlighted]; self.rotalButton.backgroundColor = [UIColor grayColor]; self.rotalButton.tag = 40; [self.rotalButton addTarget:self action:@selector(rotal) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.rotalButton]; // 设置复位按钮的属性 self.originButton = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH-30)/2+20, WIDTH+30+10, (WIDTH-30)/2, 30)]; [self.originButton setTitle:@"复位" forState:UIControlStateNormal]; [self.originButton setTitle:@"触发复位" forState:UIControlStateHighlighted]; self.originButton.backgroundColor = [UIColor grayColor]; self.originButton.tag = 50; [self.originButton addTarget:self action:@selector(origin) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.originButton]; // 设置改变颜色按钮的属性 self.changeColorButton = [[UIButton alloc] initWithFrame:CGRectMake(10, WIDTH+30+10+30+10, (WIDTH-30)/2, 30)]; [self.changeColorButton setTitle:@"改变颜色" forState:UIControlStateNormal]; [self.changeColorButton setTitle:@"触发变色" forState:UIControlStateHighlighted]; self.changeColorButton.backgroundColor = [UIColor grayColor]; self.changeColorButton.tag = 60; [self.changeColorButton addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.changeColorButton]; // 设置移除按钮的属性 self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH-30)/2+20, WIDTH+80,(WIDTH-30)/2, 30)]; [self.removeButton setTitle:@"移除" forState:UIControlStateNormal]; [self.removeButton setTitle:@"触发移除" forState:UIControlStateHighlighted]; self.removeButton.backgroundColor = [UIColor grayColor]; self.removeButton.tag = 70; [self.removeButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.removeButton]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) trans{ // 此方法用于平移showButton的位置 NSLog(@"进行一次平移"); // 以下这种方法只是在原始位置上进行平移,且只进行一次 // self.showButton.transform = CGAffineTransformMakeTranslation(0, -10); // 而第二种方法是在原来的基础之上进行平移,可无限进行 [UIView animateWithDuration:1 animations:^{ self.showButton.transform = CGAffineTransformTranslate(self.showButton.transform, 0, 15); }]; } -(void) scale;{ // 此方法用于改变showButton的大小 NSLog(@"进行一次缩放"); // 利用UIView的transform属性进行大小改变 // 下面这种方法只是在初始大小上进行大小改变,且只进行一次 // self.showButton.transform = CGAffineTransformMakeScale(0.5, 0.5); //以下第二种方法是在原来改变的基础之上进行大小改变,可无限改变 // 设置动画增强效果 [UIView animateWithDuration:1 animations:^{ self.showButton.transform = CGAffineTransformScale(self.showButton.transform, 0.5, 0.5); }]; } -(void) rotal{ // 此方法用于旋转 NSLog(@"进行一次旋转"); // 同样下面只是针对于初始位置仅仅做一次旋转 // self.showButton.transform = CGAffineTransformMakeRotation(M_PI_4); // 下面这种方法是在原来旋转位置的基础之上进行旋转,可以无限制旋转 // 加以下动画增强效果 [UIView animateWithDuration:1 animations:^{ self.showButton.transform = CGAffineTransformRotate(self.showButton.transform, M_PI_4); }]; } -(void)origin;{ // 此方法用于把showButton归位到起始位置 NSLog(@"进行一次复位"); //设置组件的transform属性 为CGAffineTransformIdentity即可复位 // 设置动画增强效果 [UIView animateWithDuration:1 animations:^{ self.showButton.transform = CGAffineTransformIdentity; }]; } -(void) change{ // 此方法用于给当前View内的所有Button组件分别设置不同的背景颜色 NSLog(@"进行一次颜色改变"); // 先进行设置父控件的背景颜色 [self.changeColorButton.superview setBackgroundColor:[UIColor greenColor]]; } -(void)remove{ // 此方法用于移出当前View内的所有控件 NSLog(@"进行一次移除"); // 先获取当前父控件中的所有子控件 NSArray *sonsOfView = self.removeButton.superview.subviews; for( UIButton * button in sonsOfView) { if (button.tag != 70) { // 如果不为当前按钮,则全部从父控件中移除 [button removeFromSuperview]; } } } @end </span>运行结果如下: