iOS的UI開發中Button的基本編寫方法講解_IOS

來源:互聯網
上載者:User

一、簡單說明

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

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

二、按鈕的三種狀態

normal(普通狀態)

預設情況(Default)

對應的枚舉常量:UIControlStateNormal


highlighted(高亮狀態)

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

對應的枚舉常量:UIControlStateHighlighted


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

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

對應的枚舉常量:UIControlStateDisabled

 
三、注意點

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

(2)若干多個控制項共用一段代碼,通常使用tag。


四、程式碼範例
(1)

複製代碼 代碼如下:

#import "LFViewController.h"

@interface LFViewController ()

@property (weak, nonatomic) IBOutlet UIButton *headImageView;

@end

@implementation LFViewController

// 在OC中,絕大多數的控制項的監聽方法的第一個參數就是控制項本身
//- (IBAction)left:(UIButton *)button {
//   
//    NSLog(@"----");
//}
- (IBAction)move
{
    // 通過frame修改head的位置
    // 在OC中,不允許直接修改“對象”的“結構體屬性”的“成員”
    // 允許修改“對象”的“結構體屬性”
    // 1. 取出結構體屬性
    CGRect rect = self.headImageView.frame;
    // 2. 修改結構體成員
    rect.origin.y -= 20;
    // 3. 設定對象的結構體屬性
    self.headImageView.frame = rect;
}

(2)

複製代碼 代碼如下:

#import "LFViewController.h"

/**
 使用git
 
 1. 建立項目時,勾選git
 2. 開發告一段落後,選擇"Source Control""Commit",並編寫注釋
 */


// 枚舉類型實質上就是一個整數,作用就是用來替代魔法數字
// 枚舉類型中,指定了第一個整數之後,後面的數字會遞增
typedef enum
{
    kMovingDirTop = 10,
    kMovingDirBottom,
    kMovingDirLeft,
    kMovingDirRight,
} kMovingDir;

#define kMovingDelta 50

@interface LFViewController ()

@property (weak, nonatomic) IBOutlet UIButton *headImageView;

@end

@implementation LFViewController

- (IBAction)move:(UIButton *)button
{
//    CGRect rect = self.headImageView.frame;
    CGPoint p = self.headImageView.center;
   
    // magic number魔法數字,其他程式員看到代碼的時候,不知道是什麼意思
    switch (button.tag) {
        case kMovingDirTop:
            p.y -= kMovingDelta;
            break;
        case kMovingDirBottom:
            p.y += kMovingDelta;
            break;
        case kMovingDirLeft:
            p.x -= kMovingDelta;
            break;
        case kMovingDirRight:
            p.x += kMovingDelta;
            break;
    }

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
   
    self.headImageView.center = p;
   
    [UIView commitAnimations];
}

- (IBAction)zoom:(UIButton *)button
{
    CGRect rect = self.headImageView.bounds;
   
    // 在C語言中,關於bool的判斷:非零即真
    if (button.tag) {
        rect.size.width += 50;
        rect.size.height += 50;
    } else {
        rect.size.width -= 50;
        rect.size.height -= 50;
    }
 
    // 首尾動畫
    // beginAnimations表示此後的代碼要“參與到”動畫中
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
   
    self.headImageView.bounds = rect;
//    self.headImageView.alpha = 0;
   
    // commitAnimations,將beginAnimation之後的所有動畫提交並產生動畫
    [UIView commitAnimations];
}

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


下面來羅列一下UIButton的基本屬性羅列
第一、UIButton的定義

複製代碼 代碼如下:

    UIButton *button=[[UIButton buttonWithType:(UIButtonType);

能夠定義的button類型有以下6種,

複製代碼 代碼如下:

 typedef enum {
 UIButtonTypeCustom = 0, 自訂風格

 UIButtonTypeRoundedRect, 圓角矩形

 UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用

 UIButtonTypeInfoLight, 亮色驚嘆號
 UIButtonTypeInfoDark, 暗色驚嘆號
UIButtonTypeContactAdd, 十字加號按鈕

 } UIButtonType;

第二、設定frame

複製代碼 代碼如下:

button1.frame = CGRectMake(20, 20, 280, 40);

[button setFrame:CGRectMake(20,20,50,50)];

第三、button背景色

複製代碼 代碼如下:

button1.backgroundColor = [UIColor clearColor];

[button setBackgroundColor:[UIColor blueColor]];

第四、state狀態

 forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現

複製代碼 代碼如下:

enum {
UIControlStateNormal = 0, 常規狀態顯現
 UIControlStateHighlighted = 1 << 0, 高亮狀態顯現
 UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現
 UIControlStateSelected = 1 << 2, 選中狀態
 UIControlStateApplication = 0x00FF0000, 當應用程式標誌時

 UIControlStateReserved = 0xFF000000 為內部架構預留,可以不管他
};

 

@property(nonatomic,getter=isEnabled)BOOL enabled;                                 // default is YES. if NO, ignores touch events and subclasses may draw differently

@property(nonatomic,getter=isSelected)BOOL selected;                               // default is NO may be used by some subclasses or by application

@property(nonatomic,getter=isHighlighted)BOOL highlighted;      

                   

第五 、設定button填充圖片和背景圖片

 

複製代碼 代碼如下:

    [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

    [buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

第六、設定button標題和標題顏色

複製代碼 代碼如下:

[button1 setTitle:@"點擊" forState:UIControlStateNormal];

 

  [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

第七、設定按鈕按下會發光

複製代碼 代碼如下:

    button.showsTouchWhenHighlighted=NO;

 

第八、添加或刪除事件處理

複製代碼 代碼如下:

[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

第九、 設定按鈕內部圖片間距和標題間距

複製代碼 代碼如下:

 UIEdgeInsets insets; // 設定按鈕內部圖片間距
 insets.top = insets.bottom = insets.right = insets.left = 10;
 bt.contentEdgeInsets = insets;
 bt.titleEdgeInsets = insets; // 標題間距

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.