国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2020-07-26 03:32:45
字體:
供稿:網(wǎng)友

一、簡單說明

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

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

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

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

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

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


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

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

對應(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)行管理,可以使用拖拽的方式添加項目中用到的圖片到Images.xcassets中

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


四、代碼示例
(1)

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

#import "LFViewController.h"

@interface LFViewController ()

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

@end

@implementation LFViewController

// 在OC中,絕大多數(shù)的控件的監(jiān)聽方法的第一個參數(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)建項目時,勾選git
 2. 開發(fā)告一段落后,選擇"Source Control""Commit",并編寫注釋
 */


// 枚舉類型實(shí)質(zhì)上就是一個整數(shù),作用就是用來替代魔法數(shù)字
// 枚舉類型中,指定了第一個整數(shù)之后,后面的數(shù)字會遞增
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ù)字,其他程序員看到代碼的時候,不知道是什么意思
    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;
    }
 
    // 首尾動畫
    // beginAnimations表示此后的代碼要“參與到”動畫中
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
   
    self.headImageView.bounds = rect;
//    self.headImageView.alpha = 0;
   
    // commitAnimations,將beginAnimation之后的所有動畫提交并生成動畫
    [UIView commitAnimations];
}

@end


五、補(bǔ)充筆記

1. IBAction的參數(shù)

- (IBAction)left:(UIButton *)button

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

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

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

 

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

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

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

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

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

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

 

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

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

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

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

 

4. frame & bounds & center

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

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

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

 

5. 首尾式動畫

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

// beginAnimations表示此后的代碼要“參與到”動畫中

[UIView beginAnimations:nil context:nil];

// setAnimationDuration用來指定動畫持續(xù)時間

[UIView setAnimationDuration:2.0];

self.headImageView.bounds = rect;

......

// commitAnimations,將beginAnimation之后的所有動畫提交并生成動畫

[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: 這個參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會顯現(xiàn)

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

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

 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è)置按鈕按下會發(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)題間距

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安国市| 平定县| 巫溪县| 临湘市| 淮北市| 金华市| 昌平区| 光泽县| 阿城市| 南阳市| 靖宇县| 临沂市| 河池市| 北流市| 酒泉市| 常熟市| 万盛区| 洱源县| 咸阳市| 呈贡县| 湄潭县| 金阳县| 丘北县| 黄冈市| 平原县| 错那县| 诸城市| 历史| 徐闻县| 洪洞县| 汉沽区| 凌云县| 马龙县| 泾阳县| 天柱县| 泗水县| 武邑县| 宕昌县| 江山市| 德江县| 化德县|