码迷,mamicode.com
首页 > 其他好文 > 详细

纯代码frame位移和伸缩

时间:2014-05-23 04:03:01      阅读:467      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

bubuko.com,布布扣
  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 andX:60 andY: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 andX:20 andY: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 andX:60 andY: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 andX:100 andY: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 andX:180 andY:320];
108     // 监听点击事件
109     [plusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside];
110     
111     // 缩小
112     UIButton *minusButton = [self createButton:@"minus" andTag:0 andX:240 andY:320];
113     // 监听点击事件
114     [minusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside];
115     
116 }
117 
118 #pragma mark - 实现方法
119 /** 创建按钮 默认和高亮的图片名称相近,x,y是位置是手动布局的 */
120 - (UIButton *) createButton:(NSString *)location andTag: (int)tag andX: (CGFloat)x andY: (CGFloat)y
121 {
122     UIButton *btn = [[UIButton alloc] init];
123     
124     btn.frame = CGRectMake(x, y, KMoveButton, KMoveButton);
125     // 背景
126     NSString *normal = [NSString stringWithFormat:@"%@_normal",location];
127     UIImage *upImage = [UIImage imageNamed:normal];
128     [btn setBackgroundImage:upImage forState:UIControlStateNormal];
129     
130     //高亮背景
131     NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted",location];
132     UIImage *upImageHighed = [UIImage imageNamed:highlighted];
133     [btn setBackgroundImage:upImageHighed forState:UIControlStateHighlighted];
134     // tag
135     btn.tag = tag;
136     [self.view addSubview:btn];
137     return btn;
138 }
139 
140 
141 /** 创建动画效果 */
142 - (void) makeAnimation : (void (^)())block
143 {
144     [UIView beginAnimations:nil context:nil];
145     [UIView setAnimationDuration:1.0];
146     
147     block();
148     
149     [UIView commitAnimations];
150 }
151 
152 /** 平移方法 */
153 - (void) move : (UIButton *)button
154 {
155     [self makeAnimation:^{
156         // 创建一个临时的CGRect
157         CGRect tempFrame = self.imageView.frame;
158         
159         // 判断平移方向
160         switch (button.tag) {
161             case KMoveDirTop: //
162                 tempFrame.origin.y -= KMovingDelta;
163                 // 平移不能超过手机屏幕
164                 if (tempFrame.origin.y < 0) {
165                     tempFrame.origin.y = 0;
166                 }
167                 break;
168             case KMoveDirLeft: //
169                 tempFrame.origin.x -= KMovingDelta;
170                 // 平移不能超过手机屏幕
171                 if (tempFrame.origin.x < 0) {
172                     tempFrame.origin.x = 0;
173                 }
174                 break;
175             case KMoveDirBottom: //
176                 tempFrame.origin.y += KMovingDelta;
177                 // 平移不能超过手机屏幕
178                 if (tempFrame.origin.y > (self.view.frame.size.height - KImageWidth)) {
179                     tempFrame.origin.y = self.view.frame.size.height -KImageWidth;
180                 }
181                 break;
182             case KMoveDirRight: //
183                 tempFrame.origin.x += KMovingDelta;
184                 // 平移不能超过手机屏幕
185                 if (tempFrame.origin.x > (self.view.frame.size.width - KImageWidth)) {
186                     tempFrame.origin.x = self.view.frame.size.width - KImageWidth;
187                 }
188                 break;
189         }
190         
191         // 重新赋值
192         self.imageView.frame = tempFrame;
193         
194         // 平移到与屏幕重合则无法点击按钮 不用三目运算符则会出现一直无法点击事件
195         self.topBtn.enabled = self.imageView.frame.origin.y ? 1 : 0;
196         self.leftBtn.enabled = self.imageView.frame.origin.x ? 1 : 0;
197         self.bottomBtn.enabled = self.imageView.frame.origin.y - (self.view.frame.size.height -KImageWidth) ? 1 : 0;
198         self.rightBtn.enabled = self.imageView.frame.origin.x - (self.view.frame.size.width - KImageWidth) ? 1 : 0;
199 
200     }];
201     
202 }
203 
204 /** 伸缩方法 */
205 - (void) scale : (UIButton *)button
206 {
207     [self makeAnimation:^{
208         // 创建临时CGRect
209         CGRect tempBounds = self.imageView.bounds;
210         // 判断放大缩小
211         if (button.tag) {
212             tempBounds.size.width *= 1.2 ;
213             tempBounds.size.height *= 1.2;
214         } else {
215             tempBounds.size.width *= 0.8;
216             tempBounds.size.height *= 0.8;
217         }
218         // 重新赋值
219         self.imageView.bounds = tempBounds;
220     }];
221 }
222 
223 
224 - (void)didReceiveMemoryWarning
225 {
226     [super didReceiveMemoryWarning];
227     // Dispose of any resources that can be recreated.
228 }
229 
230 @end
bubuko.com,布布扣

图片自己找

纯代码frame位移和伸缩,布布扣,bubuko.com

纯代码frame位移和伸缩

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/wentianblog/p/3742599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!