iOS开发UI基础—手写控件,frame,center和bounds属性
iOS開(kāi)發(fā)UI基礎(chǔ)—手寫(xiě)控件,frame,center和bounds屬性
一、手寫(xiě)控件
1.手寫(xiě)控件的步驟 (1)使用相應(yīng)的控件類(lèi)創(chuàng)建控件對(duì)象 (2)設(shè)置該控件的各種屬性 (3)添加控件到視圖中 (4)如果是button等控件,還需考慮控件的單擊事件等 (5)注意:View Contollor和view的關(guān)系 2.注意點(diǎn)在OC開(kāi)發(fā)中,Storyboard中的所有操作都可以通過(guò)代碼實(shí)現(xiàn),程序員一定要熟練掌握代碼布局界面的能力!
設(shè)置控件監(jiān)聽(tīng)方法的示例代碼如下:
[btn addTarget:self?action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
提示:
1> addTarget方法定義在UIControl類(lèi)中,這意味著可以給所有繼承自UIControl類(lèi)的對(duì)象添加監(jiān)聽(tīng)方法
2> 監(jiān)聽(tīng)方法的第一個(gè)參數(shù)就是對(duì)象本身
3> 監(jiān)聽(tīng)方法的第二個(gè)參數(shù)是監(jiān)聽(tīng)控件的事件
3.代碼示例
1 //1.使用類(lèi)創(chuàng)建一個(gè)按鈕對(duì)象2 // UIButton *headbtn=[[UIButton alloc] initWithFrame:CGRectMake(100 ,100, 100, 100)];3 //設(shè)置按鈕對(duì)象為自定義型4 UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom];5 6 //2.設(shè)置對(duì)象的各項(xiàng)屬性7 //(1)位置等通用屬性設(shè)置8 headbtn.frame=CGRectMake(100, 100, 100, 100);9 10 //(2)設(shè)置普通狀態(tài)下按鈕的屬性 11 [headbtn setBackgroundImage:[UIImage imageNamed:@"i"] forState:UIControlStateNormal]; 12 [headbtn setTitle:@"點(diǎn)我!" forState:UIControlStateNormal]; 13 [headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 14 15 //(3)設(shè)置高亮狀態(tài)下按鈕的屬性 16 [headbtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateHighlighted]; 17 [headbtn setTitle:@"還行吧~" forState:UIControlStateHighlighted]; 18 [headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 19 20 //3.把對(duì)象添加到視圖中展現(xiàn)出來(lái) 21 [self.view addSubview:headbtn]; 22 //注意點(diǎn)! 23 self.headImageView=headbtn;二、frame,center和bounds屬性
1.frame、center和bounds屬性 frame:控制位置和大小 center:控制位置(中心點(diǎn)) bounds:控制大小(以自己的左上角為原點(diǎn)) 2.注意點(diǎn)(1)通過(guò)以下屬性可以修改控件的位置
frame.origin
center
(2)通過(guò)以下屬性可以修改控件的尺寸
frame.size
bounds.size
3.代碼示例
一個(gè)控制圖片上下左右平移,縮放的程序(frame、center和bounds屬性)
1 //2 // YYViewController.m3 // 01-練習(xí)使用按鈕的frame和center屬性4 //5 // Created by apple on 14-5-21.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"10 11 //私有擴(kuò)展12 @interface YYViewController ()13 14 @property(nonatomic,weak)IBOutlet UIButton *headImageView;15 @end16 17 @implementation YYViewController18 19 //枚舉類(lèi)型,從1開(kāi)始20 typedef enum21 {22 ktopbtntag=1,23 kdownbtntag,24 krightbtntag,25 kleftbtntag26 }btntag;27 28 //viewDidLoad是視圖加載完成后調(diào)用的方法,通常在此方法中執(zhí)行視圖控制器的初始化工作29 - (void)viewDidLoad30 {31 32 //在viewDidLoad方法中,不要忘記調(diào)用父類(lèi)的方法實(shí)現(xiàn)33 [super viewDidLoad];34 35 36 //手寫(xiě)控件代碼37 //一、寫(xiě)一個(gè)按鈕控件,上面有一張圖片38 39 //1.使用類(lèi)創(chuàng)建一個(gè)按鈕對(duì)象40 // UIButton *headbtn=[[UIButton alloc] initWithFrame:CGRectMake(100 ,100, 100, 100)];41 //設(shè)置按鈕對(duì)象為自定義型42 UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom];43 44 //2.設(shè)置對(duì)象的各項(xiàng)屬性45 //(1)位置等通用屬性設(shè)置46 headbtn.frame=CGRectMake(100, 100, 100, 100);47 48 //(2)設(shè)置普通狀態(tài)下按鈕的屬性49 [headbtn setBackgroundImage:[UIImage imageNamed:@"i"] forState:UIControlStateNormal];50 [headbtn setTitle:@"點(diǎn)我!" forState:UIControlStateNormal];51 [headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];52 53 //(3)設(shè)置高亮狀態(tài)下按鈕的屬性54 [headbtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateHighlighted];55 [headbtn setTitle:@"還行吧~" forState:UIControlStateHighlighted];56 [headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];57 58 //3.把對(duì)象添加到視圖中展現(xiàn)出來(lái)59 [self.view addSubview:headbtn];60 //注意點(diǎn)!61 self.headImageView=headbtn;62 63 64 //二、寫(xiě)四個(gè)控制圖片左右上下移動(dòng)方向的按鈕控件65 66 /**================向上的按鈕=====================*/67 //1.創(chuàng)建按鈕對(duì)象68 UIButton *topbtn=[UIButton buttonWithType:UIButtonTypeCustom];69 70 //2.設(shè)置對(duì)象的屬性71 topbtn.frame=CGRectMake(100, 250, 40, 40);72 [topbtn setBackgroundImage:[UIImage imageNamed:@"top_normal"] forState:UIControlStateNormal];73 [topbtn setBackgroundImage:[UIImage imageNamed:@"top_highlighted"] forState:UIControlStateHighlighted];74 [topbtn setTag:1];75 //3.把控件添加到視圖中76 [self.view addSubview:topbtn];77 78 //4.按鈕的單擊控制事件79 [topbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];80 81 82 /**================向下的按鈕=====================*/83 //1.創(chuàng)建按鈕對(duì)象84 UIButton *downbtn=[UIButton buttonWithType:UIButtonTypeCustom];85 //2.設(shè)置對(duì)象的屬性86 downbtn.frame=CGRectMake(100, 350, 40, 40);87 [downbtn setBackgroundImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal];88 [downbtn setBackgroundImage:[UIImage imageNamed:@"bottom_highlighted"] forState:UIControlStateHighlighted];89 [downbtn setTag:2];90 //3.把控件添加到視圖中91 [self.view addSubview:downbtn];92 93 //4.按鈕的單擊控制事件94 [downbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];95 96 97 /**================向左的按鈕=====================*/98 //1.創(chuàng)建按鈕對(duì)象99 UIButton *leftbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 100 //2.設(shè)置對(duì)象的屬性 101 leftbtn.frame=CGRectMake(50, 300, 40, 40); 102 [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; 103 [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; 104 [leftbtn setTag:4]; 105 //3.把控件添加到視圖中 106 [self.view addSubview:leftbtn]; 107 108 //4.按鈕的單擊控制事件 109 [leftbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside]; 110 111 112 113 /**================向右的按鈕=====================*/ 114 //1.創(chuàng)建按鈕對(duì)象 115 UIButton *rightbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 116 //2.設(shè)置對(duì)象的屬性 117 rightbtn.frame=CGRectMake(150, 300, 40, 40); 118 [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; 119 [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; 120 [rightbtn setTag:3]; 121 //3.把控件添加到視圖中 122 [self.view addSubview:rightbtn]; 123 124 //4.按鈕的單擊控制事件 125 [rightbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside]; 126 127 //三、寫(xiě)兩個(gè)縮放按鈕 128 /**================放大的按鈕=====================*/ 129 //1.創(chuàng)建對(duì)象 130 UIButton *plusbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 131 //2.設(shè)置屬性 132 plusbtn.frame=CGRectMake(75, 400, 40, 40); 133 [plusbtn setBackgroundImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal]; 134 [plusbtn setBackgroundImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted]; 135 [plusbtn setTag:1]; 136 //3.添加到視圖 137 [self.view addSubview:plusbtn]; 138 //4.單擊事件 139 [plusbtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside]; 140 141 142 /**================縮小的按鈕=====================*/ 143 UIButton *minusbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 144 minusbtn.frame=CGRectMake(125, 400, 40, 40); 145 [minusbtn setBackgroundImage:[UIImage imageNamed:@"minus_normal"] forState:UIControlStateNormal]; 146 [minusbtn setBackgroundImage:[UIImage imageNamed:@"minus_highlighted"] forState:UIControlStateHighlighted]; 147 [minusbtn setTag:0]; 148 [self.view addSubview:minusbtn]; 149 [minusbtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside]; 150 } 151 152 //控制方向的多個(gè)按鈕調(diào)用同一個(gè)方法 153 -(void)Click:(UIButton *)button 154 { 155 156 //練習(xí)使用frame屬性 157 //CGRect frame=self.headImageView.frame; 158 159 /**注意,這里如果控制位置的兩個(gè)屬性frame和center同時(shí)使用的話(huà),會(huì)出現(xiàn)很好玩的效果,注意分析*/ 160 //練習(xí)使用center屬性 161 CGPoint center=self.headImageView.center; 162 switch (button.tag) { 163 case ktopbtntag: 164 center.y-=30; 165 break; 166 case kdownbtntag: 167 center.y+=30; 168 break; 169 case kleftbtntag: 170 //發(fā)現(xiàn)一個(gè)bug,之前的問(wèn)題是因?yàn)樯賹?xiě)了break,造成了它們的順序執(zhí)行,sorry 171 //center.x=center.x-30; 172 center.x-=50; 173 break; 174 case krightbtntag: 175 center.x+=50; 176 break; 177 } 178 179 // self.headImageView.frame=frame; 180 181 //首尾式設(shè)置動(dòng)畫(huà)效果 182 [UIView beginAnimations:nil context:nil]; 183 self.headImageView.center=center; 184 //設(shè)置時(shí)間 185 [UIView setAnimationDuration:2.0]; 186 [UIView commitAnimations]; 187 NSLog(@"移動(dòng)!"); 188 189 } 190 -(void)Zoom:(UIButton *)btn 191 { 192 //使用frame,以自己的左上角(自己的原點(diǎn))為原點(diǎn) 193 // CGRect frame=self.headImageView.frame; 194 // if (btn.tag) { 195 // frame.size.height+=30; 196 // frame.size.width+=30; 197 // } 198 // else 199 // { 200 // frame.size.width-=50; 201 // frame.size.height-=50; 202 // } 203 // self.headImageView.frame=frame; 204 205 206 //使用bounds,以中心點(diǎn)位原點(diǎn)進(jìn)行縮放 207 CGRect bounds = self.headImageView.bounds; 208 if (btn.tag) { 209 bounds.size.height+=30; 210 bounds.size.width+=30; 211 } 212 else 213 { 214 bounds.size.height-=50; 215 bounds.size.width-=50; 216 } 217 218 //設(shè)置首尾動(dòng)畫(huà) 219 [UIView beginAnimations:nil context:nil]; 220 self.headImageView.bounds=bounds; 221 [UIView setAnimationDuration:2.0]; 222 [UIView commitAnimations]; 223 } 224 @end實(shí)現(xiàn)效果:
三、簡(jiǎn)單的動(dòng)畫(huà)效果
簡(jiǎn)單介紹首尾式動(dòng)畫(huà)效果 (1)開(kāi)始動(dòng)畫(huà) (2)設(shè)置動(dòng)畫(huà)相關(guān)的時(shí)間等 (3)參與動(dòng)畫(huà)的行動(dòng) (4)提交動(dòng)畫(huà) 注:實(shí)現(xiàn)代碼參考上面的代碼?
總結(jié)
以上是生活随笔為你收集整理的iOS开发UI基础—手写控件,frame,center和bounds属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。