IOS UIButton用法

這段代碼動(dòng)態(tài)的創(chuàng)建了一個(gè)UIButton,并且把相關(guān)常用的屬性都列舉了.希望對(duì)大家有用.
//這里創(chuàng)建一個(gè)圓角矩形的按鈕
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// 能夠定義的button類(lèi)型有以下6種,
// typedef enum {
// UIButtonTypeCustom = 0, 自定義風(fēng)格
// UIButtonTypeRoundedRect, 圓角矩形
// UIButtonTypeDetailDisclosure, 藍(lán)色小箭頭按鈕,主要做詳細(xì)說(shuō)明用
// UIButtonTypeInfoLight, 亮色感嘆號(hào)
// UIButtonTypeInfoDark, 暗色感嘆號(hào)
// UIButtonTypeContactAdd, 十字加號(hào)按鈕
// } UIButtonType;

//給定button在view上的位置
button1.frame = CGRectMake(20, 20, 280, 40);

//button背景色
button1.backgroundColor = [UIColor clearColor];

//設(shè)置button填充圖片
//[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];

//設(shè)置button標(biāo)題
[button1 setTitle:@"點(diǎn)擊" forState:UIControlStateNormal];

/* forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)*/
//以下是幾種狀態(tài)
// 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ù)留,可以不管他
// };

/*

  • 默認(rèn)情況下,當(dāng)按鈕高亮的情況下,圖像的顏色會(huì)被畫(huà)深一點(diǎn),如果這下面的這個(gè)屬性設(shè)置為no,
  • 那么可以去掉這個(gè)功能
    /
    button1.adjustsImageWhenHighlighted = NO;
    /
    跟上面的情況一樣,默認(rèn)情況下,當(dāng)按鈕禁用的時(shí)候,圖像會(huì)被畫(huà)得深一點(diǎn),設(shè)置NO可以取消設(shè)置/
    button1.adjustsImageWhenDisabled = NO;
    /
    下面的這個(gè)屬性設(shè)置為yes的狀態(tài)下,按鈕按下會(huì)發(fā)光*/
    button1.showsTouchWhenHighlighted = YES;

/* 給button添加事件,事件有很多種,我會(huì)單獨(dú)開(kāi)一篇博文介紹它們,下面這個(gè)時(shí)間的意思是
按下按鈕,并且手指離開(kāi)屏幕的時(shí)候觸發(fā)這個(gè)事件,跟web中的click事件一樣。
觸發(fā)了這個(gè)事件以后,執(zhí)行butClick:這個(gè)方法,addTarget:self 的意思是說(shuō),這個(gè)方法在本類(lèi)中
也可以傳入其他類(lèi)的指針*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

//顯示控件
[self.view addSubview:button1];
注意:
[button1 addTarget:self
action:@selector(alarmTimeDone:)
forControlEvents:UIControlEventTouchUpInside
];

addTarget:self 是鏈接到self,一般都這樣設(shè)置
action:@selector(alarmTimeDone:) 時(shí)間處理函數(shù)
forControlEvents:UIControlEventTouchUpInside 控件事件處理的消息

//取消按鈕已經(jīng)添加的所有事件:(這個(gè)比較重要,若添加了兩個(gè)事件 兩個(gè)事件都會(huì)被觸發(fā))
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

何時(shí)釋放release UIButton?
是否在dealloc中對(duì)UIButton對(duì)象進(jìn)行release操作,取決于UIButton初始化的方式。
如果使用[UIButtonbuttonWithType:UIButtonTypeRoundedRect]這種方式,是不需要進(jìn)行release操作的,因?yàn)檫@種方式是自動(dòng)釋放的。如果使用 [[UIButton alloc]init]的方式,則需要主動(dòng)進(jìn)行release釋放操作。

IOS UIButton事件:

UIControlEventTouchDown
單點(diǎn)觸摸按下事件:用戶點(diǎn)觸屏幕,或者又有新手指落下的時(shí)候。
UIControlEventTouchDownRepeat
多點(diǎn)觸摸按下事件,點(diǎn)觸計(jì)數(shù)大于1:用戶按下第二、三、或第四根手指的時(shí)候。
UIControlEventTouchDragInside
當(dāng)一次觸摸在控件窗口內(nèi)拖動(dòng)時(shí)。
UIControlEventTouchDragOutside
當(dāng)一次觸摸在控件窗口之外拖動(dòng)時(shí)。
UIControlEventTouchDragEnter
當(dāng)一次觸摸從控件窗口之外拖動(dòng)到內(nèi)部時(shí)。
UIControlEventTouchDragExit
當(dāng)一次觸摸從控件窗口內(nèi)部拖動(dòng)到外部時(shí)。

UIControlEventTouchUpInside
所有在控件之內(nèi)觸摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外觸摸抬起事件(點(diǎn)觸必須開(kāi)始與控件內(nèi)部才會(huì)發(fā)送通知)。
UIControlEventTouchCancel
所有觸摸取消事件,即一次觸摸因?yàn)榉派狭颂嗍种付蝗∠?,或者被上鎖或者電話呼叫打斷。
UIControlEventTouchChanged
當(dāng)控件的值發(fā)生改變時(shí),發(fā)送通知。用于滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時(shí)發(fā)送通知,在滑塊被放下時(shí)發(fā)送,或者在被拖動(dòng)時(shí)發(fā)送。
UIControlEventEditingDidBegin
當(dāng)文本控件中開(kāi)始編輯時(shí)發(fā)送通知。
UIControlEventEditingChanged
當(dāng)文本控件中的文本被改變時(shí)發(fā)送通知。
UIControlEventEditingDidEnd
當(dāng)文本控件中編輯結(jié)束時(shí)發(fā)送通知。
UIControlEventEditingDidOnExit
當(dāng)文本控件內(nèi)通過(guò)按下回車(chē)鍵(或等價(jià)行為)結(jié)束編輯時(shí),發(fā)送通知。
UIControlEventAlltouchEvents
通知所有觸摸事件。
UIControlEventAllEditingEvents
通知所有關(guān)于文本編輯的事件。
UIControlEventAllEvents
通知所有事件。

實(shí)例:

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
    // btn.frame = rect_screen;  
    btn.frame = CGRectMake(frame.size.width - 20.0f - 30.0f, frame.size.height - 50.0f, 30.0f, 50.0f);  
    btn.backgroundColor = [UIColor blueColor];  
      
    // UIControlEventTouchDragInside  
    // UIControlEventTouchDragOutside  
      
    [btn addTarget:self action:@selector(dragInside) forControlEvents:UIControlEventTouchDragInside];      
    [btn addTarget:self action:@selector(dragOutside) forControlEvents:UIControlEventTouchDragOutside];      
    // dismissView  
    [btn addTarget:self action:@selector(upInside) forControlEvents:UIControlEventTouchUpInside];      
    [self addSubview:btn];      
    return self;  
}  
  
- (void)dragInside  
{  
    NSLog(@"dragInside...");  
}  
  
- (void)dragOutside  
{  
    NSLog(@"dragOutside...");  
}  
- (void)upInside  
{  
    NSLog(@"upInside...");  
}  

長(zhǎng)按事件

UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];  
    [aBtn setFrame:CGRectMake(40, 100, 60, 60)];  
    [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal];  
    //button點(diǎn)擊事件  
    [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];  
    //button長(zhǎng)按事件  
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)];   
    longPress.minimumPressDuration = 0.8; //定義按的時(shí)間  
    [aBtn addGestureRecognizer:longPress];  
  
-(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{  
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {  
        NSLog(@"長(zhǎng)按事件");  
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"確定刪除該模式嗎?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"刪除", nil nil];  
        [alert show];  
    }  
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 各位童鞋們, UIButton是iOS中常用的控件,下面來(lái)詳細(xì)介紹它的使用方法和以及開(kāi)發(fā)中需要注意的問(wèn)題. UIB...
    我與太陽(yáng)肩并肩閱讀 1,462評(píng)論 2 17
  • 這段代碼動(dòng)態(tài)的創(chuàng)建了一個(gè)UIButton,并且把相關(guān)常用的屬性都列舉了//這里創(chuàng)建一個(gè)圓角矩形的按鈕UIButto...
    AAup閱讀 406評(píng)論 0 1
  • UIButton的屬性方法大全 //設(shè)置自定義的按鈕 //UIButton *b...
    阿爾法代碼狗閱讀 1,187評(píng)論 0 0
  • 第一、UIButton的定義 UIButton *button=[[UIButton buttonWithType...
    Guardian灬夜閱讀 328評(píng)論 0 0
  • UIButton的創(chuàng)建和主要屬性 UIButton從字面上大家就應(yīng)該能看出來(lái)是一個(gè)可點(diǎn)擊的控件,類(lèi)似于androi...
    SankQin閱讀 1,776評(píng)論 0 0

友情鏈接更多精彩內(nèi)容