IOS開發(fā)UIButton基礎(chǔ)&事件

本節(jié)學(xué)習(xí)內(nèi)容:

1.UIButton的控件基本概念

2.UIButton的創(chuàng)建方法

3.UIButton的類型

4.可顯示圖片的UIButton

【viewController.m】

-(void)createUIRectButton{

//創(chuàng)建一個btn對象,根據(jù)類型來創(chuàng)建button

//圓角類型btn:UIButtonTypeRoundedRect

//通過類方法來創(chuàng)建ButtonWithType:類名+方法

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//設(shè)置button按鈕的位置

btn.frame=CGRectMacke(100,100,100,40);

//設(shè)置按鈕的文字內(nèi)容

// @parameter p1:字符串類型,顯示到按鈕上的文字,

//P2:設(shè)置文字顯示的狀態(tài)類型:UICountrolStateNormal,正常狀態(tài)

btn setTitle:@"按鈕01" forState:UICountrolStateNormal];

//P1顯示文字

//P2顯示狀態(tài):UICountrolStateHighLighted

btn setTitle:@"按鈕按下" forState:UICountrolStateHighLighted];

//設(shè)置button背景顏色

btn.backgroundColor=[UIColor grayColor];

//設(shè)置文字顏色

//p1 顏色,p2狀態(tài)

[btn setTitleColor:[UIColor redColor] forState:UIControlStaNromal];

//設(shè)置按下狀態(tài)的顏色

[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHiglighted];

//設(shè)置按鈕風(fēng)格顏色

[btn setTintColor:[UIColor whitecolor]];

//注:setTitleColor 優(yōu)先級高于setTintColor

//titleLabel:UILabel空控

btn,titleLable.font=[UIFount systemFontOfSize:12];

//添加到試圖中并顯示

[self.view addSubview:btn]

}

//創(chuàng)建一個可以顯示圖片btn

-(void)createImageBtn{

//創(chuàng)建一個自定義類型的btn

UIButton *btnImage=[Button vuttonWithType:UIButtonTypeCustom];

//圖片Btn位置

btnImage.frame=CGRectMake(100,200,100,100);

//默認(rèn)圖片

UIImage *icon01=[UIImage imageNamed:@"btn02.jpg"];

//點擊后的圖片

UIImage *icon02=[UIImage imageNamed:@"btn03.jpg"];

//設(shè)置按鈕圖片方法設(shè)置 p1:顯示的圖片對象,p1控件狀態(tài)

[btnImage setImage:icon01 forState:UIControlStateNormal];

[btnImage setImage:icon02 forState:UIControlStateHighlighted];?

[self.view addSubview:btnImage];

}


【UIButton事件】

本節(jié)學(xué)習(xí)內(nèi)容:

1.UIButton的事件的概念

2.UIButton的添加方法

3.UIButton的響應(yīng)函數(shù)

4.多按鈕使用同一事件函數(shù)

【viewController.h】

-(void)createBtn{

//創(chuàng)建圓角按鈕

UIButton *btn=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn.fram=CGRectMake(100,100,80,40);

[btn setTitle:@"按鈕" froState:UIControlStateNormal];

//給按鈕事件函數(shù)

//參數(shù)P1:誰來實現(xiàn)事件函數(shù),實現(xiàn)者對像都就是“誰|"

//參數(shù)P2:@selector(pressBtn)函數(shù)對像,當(dāng)按鈕滿足P3事件類型時,廟用函數(shù)

//參數(shù)p3:UIControlEvent:事件處理函數(shù)類型

//UIControlEventTouchUpinside:當(dāng)手指離開屏幕時并且手指的位置在按鈕范圍內(nèi)觸發(fā)事件函數(shù)

//UIControlEventTouchDowninside:當(dāng)手指觸摸屏幕上觸發(fā)

/*

調(diào)用不帶參數(shù)事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

/*

調(diào)用不帶參數(shù)事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

//帶參數(shù)事件

btn addTarget:self acion:@selector(pressBtn:)forCountrolEvents:UIControlEventTouchUpinside];

//確摸時調(diào)用事件函數(shù)

[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown]

[self.veiw addSubView:Btn];

UIButton *btn02=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn02.fram=CGRectMake(100,200,80,40);

[btn02 setTitle:@"按鈕" froState:UIControlStateNormal];

//可以多個按鈕使用同一個事函數(shù)來處理不同按鈕事件

[btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchDown];

[self.veiw addSubView:Btn02];

//設(shè)置按鈕標(biāo)記值

btn.tag=101;

btn02.tag=102;

-(void)pressBtn02{

if(btn.tag==101){

NSLog(@"btn 01 pressed ");

}

if(btn.tag==102){

NSLog(@"btn 02 pressed");

}

-(void)touchDown

{

NSLog(@"按鈕被觸摸!");

}

-(void)pressBtn{

NSLog(@"按鈕被按了一下!");

}

//參數(shù)為按鈕本身

-(void)pressBtn:(UIButton*)btn{

NSLog(@"tbn pressed");

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容