IOS UIButton用法詳解


這段代碼動態的創建了一個UIButton,並且把相關常用的屬性都列舉了.希望對大家有用.

  //這里創建一個圓角矩形的按鈕
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// 能夠定義的button類型有以下6種,
// typedef enum {
// UIButtonTypeCustom = 0, 自定義風格
// UIButtonTypeRoundedRect, 圓角矩形 
// UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用
// UIButtonTypeInfoLight, 亮色感嘆號
// UIButtonTypeInfoDark, 暗色感嘆號
// UIButtonTypeContactAdd, 十字加號按鈕
// } UIButtonType;

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

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

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

//設置button標題
[button1 setTitle:@"點擊" forState:UIControlStateNormal];

/* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/
//以下是幾種狀態
// enum {
// UIControlStateNormal = 0, 常規狀態顯現 
// UIControlStateHighlighted = 1 << 0, 高亮狀態顯現 
// UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現
// UIControlStateSelected = 1 << 2, 選中狀態 
// UIControlStateApplication = 0x00FF0000, 當應用程序標志時 
// UIControlStateReserved = 0xFF000000 為內部框架預留,可以不管他 
// };

/*
* 默認情況下,當按鈕高亮的情況下,圖像的顏色會被畫深一點,如果這下面的這個屬性設置為no,
* 那么可以去掉這個功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情況一樣,默認情況下,當按鈕禁用的時候,圖像會被畫得深一點,設置NO可以取消設置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的這個屬性設置為yes的狀態下,按鈕按下會發光*/
button1.showsTouchWhenHighlighted = YES;

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

//顯示控件
[self.view addSubview:button1];

注意:

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

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

 

//取消按鈕已經添加的所有事件:(這個比較重要,若添加了兩個事件  兩個事件都會被觸發)

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

 

何時釋放release UIButton?

是否在dealloc中對UIButton對象進行release操作,取決於UIButton初始化的方式。

如果使用[UIButtonbuttonWithType:UIButtonTypeRoundedRect]這種方式,是不需要進行release操作的,因為這種方式是自動釋放的。如果使用 [[UIButton alloc]init]的方式,則需要主動進行release釋放操作。

 
 
UIControlEventTouchDown

單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。

UIControlEventTouchDownRepeat

多點觸摸按下事件,點觸計數大於1:用戶按下第二、三、或第四根手指的時候。

UIControlEventTouchDragInside

當一次觸摸在控件窗口內拖動時。

UIControlEventTouchDragOutside

當一次觸摸在控件窗口之外拖動時。

UIControlEventTouchDragEnter

當一次觸摸從控件窗口之外拖動到內部時。

UIControlEventTouchDragExit

當一次觸摸從控件窗口內部拖動到外部時。

UIControlEventTouchUpInside

所有在控件之內觸摸抬起事件。

UIControlEventTouchUpOutside

所有在控件之外觸摸抬起事件(點觸必須開始與控件內部才會發送通知)。

UIControlEventTouchCancel

所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。

UIControlEventTouchChanged

當控件的值發生改變時,發送通知。用於滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。

UIControlEventEditingDidBegin

當文本控件中開始編輯時發送通知。

UIControlEventEditingChanged

當文本控件中的文本被改變時發送通知。

UIControlEventEditingDidEnd

當文本控件中編輯結束時發送通知。

UIControlEventEditingDidOnExit

當文本控件內通過按下回車鍵(或等價行為)結束編輯時,發送通知。

UIControlEventAlltouchEvents

通知所有觸摸事件。

UIControlEventAllEditingEvents

通知所有關於文本編輯的事件。

UIControlEventAllEvents

通知所有事件。


實例:    

 

 

  1.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  2.     // btn.frame = rect_screen;  
  3.     btn.frame = CGRectMake(frame.size.width - 20.0f - 30.0f, frame.size.height - 50.0f, 30.0f, 50.0f);  
  4.     btn.backgroundColor = [UIColor blueColor];  
  5.       
  6.     // UIControlEventTouchDragInside  
  7.     // UIControlEventTouchDragOutside  
  8.       
  9.     [btn addTarget:self action:@selector(dragInside) forControlEvents:UIControlEventTouchDragInside];      
  10.     [btn addTarget:self action:@selector(dragOutside) forControlEvents:UIControlEventTouchDragOutside];      
  11.     // dismissView  
  12.     [btn addTarget:self action:@selector(upInside) forControlEvents:UIControlEventTouchUpInside];      
  13.     [self addSubview:btn];      
  14.     return self;  
  15. }  
  16.   
  17. - (void)dragInside  
  18. {  
  19.     NSLog(@"dragInside...");  
  20. }  
  21.   
  22. - (void)dragOutside  
  23. {  
  24.     NSLog(@"dragOutside...");  
  25. }  
  26. - (void)upInside  
  27. {  
  28.     NSLog(@"upInside...");  
  29. }  

 

長按事件

  1. UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];  
  2.     [aBtn setFrame:CGRectMake(40, 100, 60, 60)];  
  3.     [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal];  
  4.     //button點擊事件  
  5.     [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];  
  6.     //button長按事件  
  7.     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)];   
  8.     longPress.minimumPressDuration = 0.8; //定義按的時間  
  9.     [aBtn addGestureRecognizer:longPress];  
  10.   
  11. -(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{  
  12.     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {  
  13.         NSLog(@"長按事件");  
  14.         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"確定刪除該模式嗎?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"刪除", nil nil];  
  15.         [alert show];  
  16.     }  
  17. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM