本節(jié)學(xué)習(xí)內(nèi)容:
1.UIButton的控件基本概念
2.UIButton的創(chuàng)建方法
3.UIButton的類型
4.可顯示圖片的UIButton
【viewController.m】
-(void)createUIRectButton{
//創(chuàng)建一個(gè)btn對(duì)象祥国,根據(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)先級(jí)高于setTintColor
//titleLabel:UILabel空控
btn,titleLable.font=[UIFount systemFontOfSize:12];
//添加到試圖中并顯示
[self.view addSubview:btn]
}
//創(chuàng)建一個(gè)可以顯示圖片btn
-(void)createImageBtn{
//創(chuàng)建一個(gè)自定義類型的btn
UIButton *btnImage=[Button vuttonWithType:UIButtonTypeCustom];
//圖片Btn位置
btnImage.frame=CGRectMake(100,200,100,100);
//默認(rèn)圖片
UIImage *icon01=[UIImage imageNamed:@"btn02.jpg"];
//點(diǎn)擊后的圖片
UIImage *icon02=[UIImage imageNamed:@"btn03.jpg"];
//設(shè)置按鈕圖片方法設(shè)置 p1:顯示的圖片對(duì)象,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:誰來實(shí)現(xiàn)事件函數(shù)愕够,實(shí)現(xiàn)者對(duì)像都就是“誰|"
//參數(shù)P2:@selector(pressBtn)函數(shù)對(duì)像走贪,當(dāng)按鈕滿足P3事件類型時(shí),廟用函數(shù)
//參數(shù)p3:UIControlEvent:事件處理函數(shù)類型
//UIControlEventTouchUpinside:當(dāng)手指離開屏幕時(shí)并且手指的位置在按鈕范圍內(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];
//確摸時(shí)調(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];
//可以多個(gè)按鈕使用同一個(gè)事函數(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");
}