iOS开发 -------- transform属性(形变)
一 transform属性
在OC中,通过transform属性可以修改对象的平移,比例和旋转角度
常用的创建transform结构体的方法分两大类
(1) 创建"基于控件初始位置"的形变
CGAffineTransformMakeTranslation (平移)
CGAffineTransformMakeScale (缩放)
CGAffineTransformMakeRotation (旋转)
(2) 创建"基于transform参数"的形变
CGAffineTransformTranslate
CGAffineTransformScale
CGAffineTransformRotate
在OC中,所有跟角度相关的数值,都是弧度值,180º = M_PI
正数表示顺时针旋转
负数代表逆时针旋转
二 代码示例
1 // 2 // RootViewController.m 3 // 练习使用按钮的frame和center属性 4 // 5 // Created by lovestarfish on 15/11/1. 6 // Copyright © 2015年 S&G. All rights reserved. 7 // 8 9 #import "RootViewController.h" 10 11 @interface RootViewController () 12 13 @property (nonatomic,retain) UIButton *headImageView; 14 15 @end 16 17 @implementation RootViewController 18 19 - (void)dealloc { 20 self.headImageView = nil; 21 [super dealloc]; 22 } 23 24 //枚举类型,从1开始 25 //枚举类型有一个很大的作用,就是用来代替程序中的魔法数字 26 typedef enum { 27 ktopbtntag = 100, 28 kdownbtntag, 29 kleftbtntag, 30 krightbtntag 31 }btntag; 32 33 /** 34 * viewDidLoad是视图加载完成后调用的方法,通常在此方法中执行视图控制器的初始工作 35 */ 36 - (void)viewDidLoad { 37 38 39 //在viewDidLoad方法中,不要忘记调用父类的方法实现 40 [super viewDidLoad]; 41 42 self.title = @"按钮的frame和center属性"; 43 44 //一 手写控件代码 45 //1.使用类创建一个按钮对象,设置按钮对象为自定义型 46 UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 47 48 //2.设置对象的各项属性 49 //(1)位置等通用属性设置 50 headBtn.frame = CGRectMake(137.5, 100, 100, 100); 51 52 //(2)设置普通状态下按钮的属性 53 [headBtn setBackgroundImage:[UIImage imageNamed:@"xib1"] forState:UIControlStateNormal]; 54 [headBtn setTitle:@"点我" forState:UIControlStateNormal]; 55 [headBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 56 57 //(3)设置高亮状态下按钮的属性 58 [headBtn setBackgroundImage:[UIImage imageNamed:@"xib2"] forState:UIControlStateHighlighted]; 59 [headBtn setTitle:@"还行吧" forState:UIControlStateHighlighted]; 60 [headBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 61 62 //3.把对象添加到视图中展现出来 63 [self.view addSubview:headBtn]; 64 self.headImageView = headBtn; 65 66 //二 写四个控制图片上下左右移动方向的控制按钮 67 //向上 68 UIButton *topBut = [UIButton buttonWithType:UIButtonTypeSystem]; 69 topBut.frame = CGRectMake(167.5, 250, 40, 40); 70 [topBut setTitle:@"向上" forState:UIControlStateNormal]; 71 topBut.tag = 100; 72 [topBut addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 73 [self.view addSubview:topBut]; 74 75 //向下 76 UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 77 downBtn.frame = CGRectMake(167.5, 350, 40, 40); 78 [downBtn setTitle:@"向下" forState:UIControlStateNormal]; 79 downBtn.tag = 101; 80 [downBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 81 [self.view addSubview:downBtn]; 82 83 //向左 84 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 85 leftBtn.frame = CGRectMake(100, 300, 40, 40); 86 [leftBtn setTitle:@"向左" forState:UIControlStateNormal]; 87 leftBtn.tag = 102; 88 [leftBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 89 [self.view addSubview:leftBtn]; 90 91 //向右 92 UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 93 rightBtn.frame = CGRectMake(240, 300, 40, 40); 94 [rightBtn setTitle:@"向右" forState:UIControlStateNormal]; 95 rightBtn.tag = 103; 96 [rightBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 97 [self.view addSubview:rightBtn]; 98 99 //三 写两个缩放按钮 100 //放大 101 UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 102 plusBtn.frame = CGRectMake(100, 400, 40, 40); 103 [plusBtn setTitle:@"放大" forState:UIControlStateNormal]; 104 plusBtn.tag = 1; 105 [plusBtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside]; 106 [self.view addSubview:plusBtn]; 107 //缩小 108 UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 109 minusBtn.frame = CGRectMake(240, 400, 40, 40); 110 [minusBtn setTitle:@"缩小" forState:UIControlStateNormal]; 111 minusBtn.tag = 0; 112 [minusBtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside]; 113 [self.view addSubview:minusBtn]; 114 115 //向左旋转按钮 116 UIButton *leftRotateBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 117 leftRotateBtn.frame = CGRectMake(137.5, 460, 100, 40); 118 [leftRotateBtn setTitle:@"向左旋转" forState:UIControlStateNormal]; 119 leftRotateBtn.tag = 1; 120 [leftRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside]; 121 [self.view addSubview:leftRotateBtn]; 122 //向右旋转按钮 123 UIButton *rightRotateBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 124 rightRotateBtn.frame = CGRectMake(137.5, 520, 100, 40); 125 [rightRotateBtn setTitle:@"向右旋转" forState:UIControlStateNormal]; 126 rightRotateBtn.tag = 0; 127 [rightRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside]; 128 [self.view addSubview:rightRotateBtn]; 129 130 } 131 132 /** 133 * 控制方向的多个按钮调用同一个方法 134 */ 135 - (void)click:(UIButton *)button { 136 CGPoint center = self.headImageView.center; 137 switch (button.tag) { 138 case ktopbtntag: 139 center.y -= 30; 140 break; 141 case kdownbtntag: 142 center.y += 30; 143 break; 144 case kleftbtntag: 145 center.x -= 50; 146 break; 147 case krightbtntag: 148 center.x += 50; 149 break; 150 default: 151 break; 152 } 153 //设置首尾动画 154 [UIView beginAnimations:nil context:nil]; 155 self.headImageView.center = center; 156 //设置时间 157 [UIView setAnimationDuration:2.0]; 158 [UIView commitAnimations]; 159 NSLog(@"移动!"); 160 } 161 162 /** 163 * 缩放 164 */ 165 - (void)zoom:(UIButton *)button { 166 //使用frame,以自己的左上角(自己的原点)为原点 167 /* 168 CGRect frame = self.headImageView.frame; 169 if (button.tag) { 170 frame.size.height += 30; 171 frame.size.width += 30; 172 } else { 173 frame.size.width -= 50; 174 frame.size.height -= 50; 175 } 176 self.headImageView.frame = frame; 177 */ 178 179 //使用bounds,以中心点为原点进行缩放 180 CGRect bounds = self.headImageView.bounds; 181 if (button.tag) { 182 bounds.size.height += 30; 183 bounds.size.width += 30; 184 } else { 185 bounds.size.height -= 50; 186 bounds.size.width -= 50; 187 } 188 189 //设置首尾动画 190 [UIView beginAnimations:nil context:nil]; 191 self.headImageView.bounds = bounds; 192 [UIView setAnimationDuration:2.0]; 193 [UIView commitAnimations]; 194 } 195 196 /** 197 * 旋转 198 */ 199 - (void)rotate:(UIButton *)button { 200 //位移(不累加) 201 // self.headImageView.transform = CGAffineTransformMakeTranslation(50, 200); 202 //缩放 203 // self.headImageView.transform = CGAffineTransformMakeScale(1.2, 0.5); 204 //在原有的基础上位移(是累加的) 205 // self.headImageView.transform = CGAffineTransformTranslate(self.headImageView.transform, 50, 50); 206 //在原有的基础上进行缩放 207 // self.headImageView.transform = CGAffineTransformScale(self.headImageView.transform, 1.5, 2.0); 208 209 //在原有的基础上进行旋转 210 if (button.tag) { 211 //旋转的角度为1/pi,逆时针 212 self.headImageView.transform = CGAffineTransformRotate(self.headImageView.transform, -M_1_PI); 213 } else { 214 //旋转的角度为pi/2,顺时针 215 self.headImageView.transform = CGAffineTransformRotate(self.headImageView.transform, M_PI_2); 216 } 217 218 } 219 220 - (void)didReceiveMemoryWarning { 221 [super didReceiveMemoryWarning]; 222 } 223 224 @end
三 实现效果