标签:style class blog c code java
1 // 2 // LWTViewController.m 3 // 纯代码位移和伸缩作业 4 // 5 // Created by apple on 14-5-20. 6 // Copyright (c) 2014年 lwt. All rights reserved. 7 // 8 9 #import "LWTViewController.h" 10 #pragma mark 定义常量 11 /** 图片距离顶部的高度 */ 12 #define KImageY 60 13 /** 图片的尺寸 */ 14 #define KImageWidth 100 15 /** 按钮的尺寸 */ 16 #define KMoveButton 40 17 /** 图片平移的距离 */ 18 #define KMovingDelta 20 19 20 /** 定义平移tag的枚举 */ 21 typedef enum { 22 KMoveDirTop = 1, 23 KMoveDirLeft, 24 KMoveDirBottom, 25 KMoveDirRight 26 } KMoveDir; 27 28 @interface LWTViewController () 29 30 /** 图片的属性 */ 31 @property (nonatomic, strong) UIButton *imageView; 32 33 /** 平移按钮的属性 */ 34 @property (nonatomic, strong) UIButton *topBtn; 35 @property (nonatomic, strong) UIButton *leftBtn; 36 @property (nonatomic, strong) UIButton *bottomBtn; 37 @property (nonatomic, strong) UIButton *rightBtn; 38 39 @end 40 41 @implementation LWTViewController 42 43 44 - (void)viewDidLoad 45 { 46 [super viewDidLoad]; 47 // Do any additional setup after loading the view, typically from a nib. 48 49 // 创建要移动的图片按钮 50 UIButton *imageViewButton = [[UIButton alloc] init]; 51 // 按钮位置 52 CGFloat imageX = (self.view.frame.size.width - KImageWidth) / 2; 53 imageViewButton.frame = CGRectMake(imageX, KImageY, KImageWidth, KImageWidth); 54 55 // 按钮默认背景 56 UIImage *image = [UIImage imageNamed:@"btn_01"]; 57 [imageViewButton setBackgroundImage:image forState:UIControlStateNormal]; 58 59 // 按钮默认文字 60 [imageViewButton setTitle:@"点我啊" forState:UIControlStateNormal]; 61 [imageViewButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 62 63 // 按钮高亮背景 64 UIImage *imageHighed = [UIImage imageNamed:@"btn_02"]; 65 [imageViewButton setBackgroundImage:imageHighed forState:UIControlStateHighlighted]; 66 67 // 按钮高亮文字 68 [imageViewButton setTitle:@"点我干啥" forState:UIControlStateHighlighted]; 69 [imageViewButton setTitleColor:[UIColor magentaColor] forState:UIControlStateHighlighted]; 70 71 [self.view addSubview:imageViewButton]; 72 73 _imageView = imageViewButton; 74 75 // 创建移动按钮 76 77 // 上 78 UIButton *topButton = [self createButton:@"top" andTag:KMoveDirTop andPoint:CGPointMake(60, 320)]; 79 // 监听点击事件 80 [topButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; 81 // 属性赋值 82 self.topBtn = topButton; 83 84 //左 85 UIButton *leftButton = [self createButton:@"left" andTag:KMoveDirLeft andPoint:CGPointMake(20, 360)]; 86 // 监听点击事件 87 [leftButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; 88 // 属性赋值 89 self.leftBtn = leftButton; 90 91 //下 92 UIButton *bottomButton = [self createButton:@"bottom" andTag:KMoveDirBottom andPoint:CGPointMake(60, 400)]; 93 // 监听点击事件 94 [bottomButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; 95 // 属性赋值 96 self.bottomBtn = bottomButton; 97 98 //右 99 UIButton *rightButton = [self createButton:@"right" andTag:KMoveDirRight andPoint:CGPointMake(100, 360)]; 100 // 监听点击事件 101 [rightButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; 102 // 属性赋值 103 self.rightBtn = rightButton; 104 105 // 创建伸缩按钮 106 // 放大 107 UIButton *plusButton = [self createButton:@"plus" andTag:1 andPoint:CGPointMake(180, 320)]; 108 // 监听点击事件 109 [plusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside]; 110 111 // 缩小 112 UIButton *minusButton = [self createButton:@"minus" andTag:0 andPoint:CGPointMake(240, 320)]; 113 // 监听点击事件 114 [minusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside]; 115 116 // 创建旋转按钮 117 //逆时针 118 UIButton *leftRotateButton = [self createButton:@"left_rotate" andTag:-1 andPoint:CGPointMake(180, 400)]; 119 // 监听点击事件 120 [leftRotateButton addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside]; 121 122 //顺时针 123 UIButton *rightRotateButton = [self createButton:@"right_rotate" andTag:1 andPoint:CGPointMake(240, 400)]; 124 // 监听点击事件 125 [rightRotateButton addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside]; 126 127 } 128 129 #pragma mark - 实现方法 130 /** 创建按钮 默认和高亮的图片名称相近,x,y是位置是手动布局的 */ 131 - (UIButton *) createButton:(NSString *)location andTag: (int)tag andPoint : (CGPoint)point 132 { 133 UIButton *btn = [[UIButton alloc] init]; 134 135 btn.frame = (CGRect){point,KMoveButton, KMoveButton}; 136 // 背景 137 NSString *normal = [NSString stringWithFormat:@"%@_normal",location]; 138 UIImage *upImage = [UIImage imageNamed:normal]; 139 [btn setBackgroundImage:upImage forState:UIControlStateNormal]; 140 141 //高亮背景 142 NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted",location]; 143 UIImage *upImageHighed = [UIImage imageNamed:highlighted]; 144 [btn setBackgroundImage:upImageHighed forState:UIControlStateHighlighted]; 145 // tag 146 btn.tag = tag; 147 [self.view addSubview:btn]; 148 return btn; 149 } 150 151 152 /** 创建动画效果 */ 153 - (void) makeAnimation : (void (^)())block 154 { 155 [UIView beginAnimations:nil context:nil]; 156 [UIView setAnimationDuration:1.0]; 157 158 block(); 159 160 [UIView commitAnimations]; 161 } 162 163 /** 平移方法 */ 164 - (void) move : (UIButton *)button 165 { 166 [self makeAnimation:^{ 167 168 // 判断平移方向 169 switch (button.tag) { 170 case KMoveDirTop: // 上 171 172 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0, -KMovingDelta); 173 // // 不能超过屏幕 174 // if (self.imageView.frame.origin.y < 0) { 175 // CGRect tempFrame = self.imageView.frame; 176 // tempFrame.origin.y = 0; 177 // self.imageView.frame = tempFrame; 178 // } 179 break; 180 case KMoveDirLeft: // 左 181 182 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -KMovingDelta, 0); 183 // // 不能超过屏幕 184 // if (self.imageView.frame.origin.x < 0) { 185 // CGRect tempFrame = self.imageView.frame; 186 // tempFrame.origin.x = 0; 187 // self.imageView.frame = tempFrame; 188 // } 189 break; 190 case KMoveDirBottom: // 下 191 192 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0, KMovingDelta); 193 // // 不能超过屏幕 194 // if (self.imageView.frame.origin.y > (self.view.frame.size.height -KImageWidth)) { 195 // CGRect tempFrame = self.imageView.frame; 196 // tempFrame.origin.y = self.view.frame.size.height -KImageWidth; 197 // self.imageView.frame = tempFrame; 198 // } 199 break; 200 case KMoveDirRight: // 右 201 202 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, KMovingDelta, 0); 203 // // 不能超过屏幕 204 // if (self.imageView.frame.origin.x > (self.view.frame.size.width - KImageWidth)) { 205 // CGRect tempFrame = self.imageView.frame; 206 // tempFrame.origin.x = self.view.frame.size.width - KImageWidth; 207 // self.imageView.frame = tempFrame; 208 // } 209 break; 210 } 211 212 // // 平移到与屏幕重合则无法点击按钮 不用三目运算符则会出现一直无法点击事件 213 // self.topBtn.enabled = self.imageView.frame.origin.y ? 1 : 0; 214 // self.leftBtn.enabled = self.imageView.frame.origin.x ? 1 : 0; 215 // self.bottomBtn.enabled = self.imageView.frame.origin.y - (self.view.frame.size.height -KImageWidth) ? 1 : 0; 216 // self.rightBtn.enabled = self.imageView.frame.origin.x - (self.view.frame.size.width - KImageWidth) ? 1 : 0; 217 218 }]; 219 220 } 221 222 /** 伸缩方法 */ 223 - (void) scale : (UIButton *)button 224 { 225 [self makeAnimation:^{ 226 // 判断放大缩小 227 CGFloat scale = button.tag ? 1.2 : 0.8; 228 229 self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale); 230 }]; 231 } 232 233 /** 旋转方法 */ 234 - (void) rotate : (UIButton *)button 235 { 236 [self makeAnimation:^{ 237 CGFloat rotate = M_PI_4 * button.tag; 238 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotate); 239 }]; 240 241 } 242 243 244 - (void)didReceiveMemoryWarning 245 { 246 [super didReceiveMemoryWarning]; 247 // Dispose of any resources that can be recreated. 248 } 249 250 @end
如果有人看到的话,麻烦你教教我transform不用frame如何设置不能超出屏幕,而且旋转后移动要边上图片会丢失。请指教,谢谢!!!
纯代码transform位移伸缩和旋转,布布扣,bubuko.com
标签:style class blog c code java
原文地址:http://www.cnblogs.com/wentianblog/p/3742607.html