ios開發中UIButton的使用(一) – iPhone手機開發技術文章 iPhone軟體開發教學課程

文頂頂

ios開發中UIButton的使用(一)

 

ios開發中UIButton的使用(一)

 

一、簡單說明

 

一般情況下,點擊某個控件後,會做出相應反應的都是按鈕

 

按鈕的功能比較多,既能顯示文字,又能顯示圖片,還能隨時調整內部圖片和文字的位置

 

二、按鈕的三種狀態

 

normal(普通狀態)

 

默認情況(Default)

 

對應的枚舉常量:UIControlStateNormal

 

 

 

highlighted(高亮狀態)

 

按鈕被按下去的時候(手指還未松開)

 

對應的枚舉常量:UIControlStateHighlighted

 

 

 

disabled(失效狀態,不可用狀態)

 

如果enabled屬性為NO,就是處於disable狀態,代表按鈕不可以被點擊

 

對應的枚舉常量:UIControlStateDisabled

 

 

 

三、註意點

 

(1)從Xcode5開始,圖片資源都放到Images.xcassets中進行管理,可以使用拖拽的方式添加項目中用到的圖片到Images.xcassets中

 

(2)若幹多個控件共用一段代碼,通常使用tag。

 

四、代碼示例

 

(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

復制代碼

 

 

五、補充筆記

 

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];

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *