這段代碼動態的創建了一個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釋放操作。
單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat
多點觸摸按下事件,點觸計數大於1:用戶按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside
當一次觸摸在控件窗口內拖動時。
UIControlEventTouchDragOutside
當一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter
當一次觸摸從控件窗口之外拖動到內部時。
UIControlEventTouchDragExit
當一次觸摸從控件窗口內部拖動到外部時。
UIControlEventTouchUpInside
所有在控件之內觸摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外觸摸抬起事件(點觸必須開始與控件內部才會發送通知)。
UIControlEventTouchCancel
所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。
UIControlEventTouchChanged
當控件的值發生改變時,發送通知。用於滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。
UIControlEventEditingDidBegin
當文本控件中開始編輯時發送通知。
UIControlEventEditingChanged
當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd
當文本控件中編輯結束時發送通知。
UIControlEventEditingDidOnExit
當文本控件內通過按下回車鍵(或等價行為)結束編輯時,發送通知。
UIControlEventAlltouchEvents
通知所有觸摸事件。
UIControlEventAllEditingEvents
通知所有關於文本編輯的事件。
UIControlEventAllEvents
通知所有事件。
實例:
- 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...");
- }
長按事件
- UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [aBtn setFrame:CGRectMake(40, 100, 60, 60)];
- [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal];
- //button點擊事件
- [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];
- //button長按事件
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)];
- longPress.minimumPressDuration = 0.8; //定義按的時間
- [aBtn addGestureRecognizer:longPress];
- -(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{
- if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
- NSLog(@"長按事件");
- UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"確定刪除該模式嗎?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"刪除", nil nil];
- [alert show];
- }
- }