亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS的UI開發(fā)中Button的基本編寫方法講解

 更新時(shí)間:2015年11月03日 09:29:27   作者:文頂頂  
這篇文章主要介紹了iOS的UI開發(fā)中Button的基本編寫方法講解,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

一、簡單說明

一般情況下,點(diǎn)擊某個(gè)控件后,會(huì)做出相應(yīng)反應(yīng)的都是按鈕

按鈕的功能比較多,既能顯示文字,又能顯示圖片,還能隨時(shí)調(diào)整內(nèi)部圖片和文字的位置

二、按鈕的三種狀態(tài)

normal(普通狀態(tài))

默認(rèn)情況(Default)

對應(yīng)的枚舉常量:UIControlStateNormal


highlighted(高亮狀態(tài))

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

對應(yīng)的枚舉常量:UIControlStateHighlighted


disabled(失效狀態(tài),不可用狀態(tài))

如果enabled屬性為NO,就是處于disable狀態(tài),代表按鈕不可以被點(diǎn)擊

對應(yīng)的枚舉常量:UIControlStateDisabled

 
三、注意點(diǎn)

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

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


四、代碼示例
(1)

復(fù)制代碼 代碼如下:

#import "LFViewController.h"

@interface LFViewController ()

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

@end

@implementation LFViewController

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

(2)

復(fù)制代碼 代碼如下:

#import "LFViewController.h"

/**
 使用git
 
 1. 創(chuàng)建項(xiàng)目時(shí),勾選git
 2. 開發(fā)告一段落后,選擇"Source Control""Commit",并編寫注釋
 */


// 枚舉類型實(shí)質(zhì)上就是一個(gè)整數(shù),作用就是用來替代魔法數(shù)字
// 枚舉類型中,指定了第一個(gè)整數(shù)之后,后面的數(shù)字會(huì)遞增
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魔法數(shù)字,其他程序員看到代碼的時(shí)候,不知道是什么意思
    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語言中,關(guān)于bool的判斷:非零即真
    if (button.tag) {
        rect.size.width += 50;
        rect.size.height += 50;
    } else {
        rect.size.width -= 50;
        rect.size.height -= 50;
    }
 
    // 首尾動(dòng)畫
    // beginAnimations表示此后的代碼要“參與到”動(dòng)畫中
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
   
    self.headImageView.bounds = rect;
//    self.headImageView.alpha = 0;
   
    // commitAnimations,將beginAnimation之后的所有動(dòng)畫提交并生成動(dòng)畫
    [UIView commitAnimations];
}

@end


五、補(bǔ)充筆記

1. IBAction的參數(shù)

- (IBAction)left:(UIButton *)button

(1) 在OC中,絕大多數(shù)的控件監(jiān)聽方法的第一個(gè)參數(shù)就是控件本身

(2) 默認(rèn)連線時(shí)的參數(shù)類型是id

(3) 如果要在監(jiān)聽方法中,方便控件的使用,可以在連線時(shí)或者連線后,修改監(jiān)聽方法的參數(shù)類型

 

2. 修改對象的結(jié)構(gòu)體成員

在OC中,不允許直接修改“對象”的“結(jié)構(gòu)體屬性”的“成員”,但是允許修改“對象”的“結(jié)構(gòu)體屬性”

修改結(jié)構(gòu)體屬性的成員方法如下:

(1)使用臨時(shí)變量記錄對象的結(jié)構(gòu)體屬性

(2) 修改臨時(shí)變量的屬性

(3)將臨時(shí)變量重新設(shè)置給對象的結(jié)構(gòu)體屬性

 

3. 在程序開發(fā)中需要避免出現(xiàn)魔法數(shù)字(Magic Number)

使用枚舉類型,可以避免在程序中出現(xiàn)魔法數(shù)字

(1)枚舉類型實(shí)質(zhì)上就是一個(gè)整數(shù),其作用就是用來替代魔法數(shù)字

(2)枚舉類型中,指定了第一個(gè)整數(shù)之后,后面的數(shù)字會(huì)遞增

 

4. frame & bounds & center

1> frame可以修改對象的位置和尺寸

2> bounds可以修改對象的尺寸

3> center可以修改對象的位置

 

5. 首尾式動(dòng)畫

復(fù)制代碼 代碼如下:

// beginAnimations表示此后的代碼要“參與到”動(dòng)畫中

[UIView beginAnimations:nil context:nil];

// setAnimationDuration用來指定動(dòng)畫持續(xù)時(shí)間

[UIView setAnimationDuration:2.0];

self.headImageView.bounds = rect;

......

// commitAnimations,將beginAnimation之后的所有動(dòng)畫提交并生成動(dòng)畫

[UIView commitAnimations];


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

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

 typedef enum {
 UIButtonTypeCustom = 0, 自定義風(fēng)格

 UIButtonTypeRoundedRect, 圓角矩形

 UIButtonTypeDetailDisclosure, 藍(lán)色小箭頭按鈕,主要做詳細(xì)說明用

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

 } UIButtonType;

第二、設(shè)置frame

復(fù)制代碼 代碼如下:

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

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

第三、button背景色

復(fù)制代碼 代碼如下:

button1.backgroundColor = [UIColor clearColor];

[button setBackgroundColor:[UIColor blueColor]];

第四、state狀態(tài)

 forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)

復(fù)制代碼 代碼如下:

enum {
UIControlStateNormal = 0, 常規(guī)狀態(tài)顯現(xiàn)
 UIControlStateHighlighted = 1 << 0, 高亮狀態(tài)顯現(xiàn)
 UIControlStateDisabled = 1 << 1, 禁用的狀態(tài)才會(huì)顯現(xiàn)
 UIControlStateSelected = 1 << 2, 選中狀態(tài)
 UIControlStateApplication = 0x00FF0000, 當(dāng)應(yīng)用程序標(biāo)志時(shí)

 UIControlStateReserved = 0xFF000000 為內(nèi)部框架預(yù)留,可以不管他
};

 

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

                   

第五 、設(shè)置button填充圖片和背景圖片

 

復(fù)制代碼 代碼如下:

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

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

第六、設(shè)置button標(biāo)題和標(biāo)題顏色

復(fù)制代碼 代碼如下:

[button1 setTitle:@"點(diǎn)擊" forState:UIControlStateNormal];

 

  [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

第七、設(shè)置按鈕按下會(huì)發(fā)光

復(fù)制代碼 代碼如下:

    button.showsTouchWhenHighlighted=NO;

 

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

復(fù)制代碼 代碼如下:

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

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

第九、 設(shè)置按鈕內(nèi)部圖片間距和標(biāo)題間距

復(fù)制代碼 代碼如下:

 UIEdgeInsets insets; // 設(shè)置按鈕內(nèi)部圖片間距
 insets.top = insets.bottom = insets.right = insets.left = 10;
 bt.contentEdgeInsets = insets;
 bt.titleEdgeInsets = insets; // 標(biāo)題間距

相關(guān)文章

最新評論