iOS開發UI基礎—Button基礎

來源:互聯網
上載者:User

iOS開發UI基礎—Button基礎

一、簡單說明

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

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

二、按鈕的三種狀態

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 LFViewController10 11 // 在OC中,絕大多數的控制項的監聽方法的第一個參數就是控制項本身12 //- (IBAction)left:(UIButton *)button {13 //    14 //    NSLog(@"----");15 //}16 - (IBAction)move17 {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 enum14 {15     kMovingDirTop = 10,16     kMovingDirBottom,17     kMovingDirLeft,18     kMovingDirRight,19 } kMovingDir;20 21 #define kMovingDelta 5022 23 @interface LFViewController ()24 25 @property (weak, nonatomic) IBOutlet UIButton *headImageView;26 27 @end28 29 @implementation LFViewController30 31 - (IBAction)move:(UIButton *)button32 {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 *)button61 {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];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.