iOS開發UI篇—Button基礎git
1、簡單說明程序員
通常狀況下,點擊某個控件後,會作出相應反應的都是按鈕動畫
按鈕的功能比較多,既能顯示文字,又能顯示圖片,還能隨時調整內部圖片和文字的位置atom
2、按鈕的三種狀態spa
normal(普通狀態)code
默認狀況(Default)orm
對應的枚舉常量:UIControlStateNormal對象
highlighted(高亮狀態)blog
按鈕被按下去的時候(手指還未鬆開)圖片
對應的枚舉常量:UIControlStateHighlighted
disabled(失效狀態,不可用狀態)
若是enabled屬性爲NO,就是處於disable狀態,表明按鈕不能夠被點擊
對應的枚舉常量:UIControlStateDisabled
3、注意點
(1)從Xcode5開始,圖片資源都放到Images.xcassets中進行管理,可使用拖拽的方式添加項目中用到的圖片到Images.xcassets中
(2)若干多個控件共用一段代碼,一般使用tag。
4、代碼示例
(1)
1 #import "LFViewController.h" 2 3 @interface LFViewController () 4 5 @property (weak, nonatomic) IBOutlet UIButton *headImageView; 6 7 @end 8 9 @implementation LFViewController 10 11 // 在OC中,絕大多數的控件的監聽方法的第一個參數就是控件自己 12 //- (IBAction)left:(UIButton *)button { 13 // 14 // NSLog(@"----"); 15 //} 16 - (IBAction)move 17 { 18 // 經過frame修改head的位置 19 // 在OC中,不容許直接修改「對象」的「結構體屬性」的「成員」 20 // 容許修改「對象」的「結構體屬性」 21 // 1. 取出結構體屬性 22 CGRect rect = self.headImageView.frame; 23 // 2. 修改結構體成員 24 rect.origin.y -= 20; 25 // 3. 設置對象的結構體屬性 26 self.headImageView.frame = rect; 27 }
(2)
1 #import "LFViewController.h" 2 3 /** 4 使用git 5 6 1. 建立項目時,勾選git 7 2. 開發告一段落後,選擇"Source Control""Commit",並編寫註釋 8 */ 9 10 11 // 枚舉類型實質上就是一個整數,做用就是用來替代魔法數字 12 // 枚舉類型中,指定了第一個整數以後,後面的數字會遞增 13 typedef enum 14 { 15 kMovingDirTop = 10, 16 kMovingDirBottom, 17 kMovingDirLeft, 18 kMovingDirRight, 19 } kMovingDir; 20 21 #define kMovingDelta 50 22 23 @interface LFViewController () 24 25 @property (weak, nonatomic) IBOutlet UIButton *headImageView; 26 27 @end 28 29 @implementation LFViewController 30 31 - (IBAction)move:(UIButton *)button 32 { 33 // CGRect rect = self.headImageView.frame; 34 CGPoint p = self.headImageView.center; 35 36 // magic number魔法數字,其餘程序員看到代碼的時候,不知道是什麼意思 37 switch (button.tag) { 38 case kMovingDirTop: 39 p.y -= kMovingDelta; 40 break; 41 case kMovingDirBottom: 42 p.y += kMovingDelta; 43 break; 44 case kMovingDirLeft: 45 p.x -= kMovingDelta; 46 break; 47 case kMovingDirRight: 48 p.x += kMovingDelta; 49 break; 50 } 51 52 [UIView beginAnimations:nil context:nil]; 53 [UIView setAnimationDuration:1.0]; 54 55 self.headImageView.center = p; 56 57 [UIView commitAnimations]; 58 } 59 60 - (IBAction)zoom:(UIButton *)button 61 { 62 CGRect rect = self.headImageView.bounds; 63 64 // 在C語言中,關於bool的判斷:非零即真 65 if (button.tag) { 66 rect.size.width += 50; 67 rect.size.height += 50; 68 } else { 69 rect.size.width -= 50; 70 rect.size.height -= 50; 71 } 72 73 // 首尾動畫 74 // beginAnimations表示此後的代碼要「參與到」動畫中 75 [UIView beginAnimations:nil context:nil]; 76 [UIView setAnimationDuration:2.0]; 77 78 self.headImageView.bounds = rect; 79 // self.headImageView.alpha = 0; 80 81 // commitAnimations,將beginAnimation以後的全部動畫提交併生成動畫 82 [UIView commitAnimations]; 83 } 84 85 @end
5、補充筆記
1. IBAction的參數
- (IBAction)left:(UIButton *)button
(1) 在OC中,絕大多數的控件監聽方法的第一個參數就是控件自己
(2) 默認連線時的參數類型是id
(3) 若是要在監聽方法中,方便控件的使用,能夠在連線時或者連線後,修改監聽方法的參數類型
2. 修改對象的結構體成員
在OC中,不容許直接修改「對象」的「結構體屬性」的「成員」,可是容許修改「對象」的「結構體屬性」
修改結構體屬性的成員方法以下:
(1)使用臨時變量記錄對象的結構體屬性
(2) 修改臨時變量的屬性
(3)將臨時變量從新設置給對象的結構體屬性
3. 在程序開發中須要避免出現魔法數字(Magic Number)
使用枚舉類型,能夠避免在程序中出現魔法數字
(1)枚舉類型實質上就是一個整數,其做用就是用來替代魔法數字
(2)枚舉類型中,指定了第一個整數以後,後面的數字會遞增
4. frame & bounds & center
1> frame能夠修改對象的位置和尺寸
2> bounds能夠修改對象的尺寸
3> center能夠修改對象的位置
5. 首尾式動畫
// beginAnimations表示此後的代碼要「參與到」動畫中
[UIView beginAnimations:nil context:nil];
// setAnimationDuration用來指定動畫持續時間
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
......
// commitAnimations,將beginAnimation以後的全部動畫提交併生成動畫
[UIView commitAnimations];