標籤:
這段代碼動態建立了一個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釋放操作。
IOS UIButton事件: UIControlEventTouchDown
單點觸摸按下事件:使用者點觸螢幕,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat
多點觸摸按下事件,點觸計數大於1:使用者按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside
當一次觸摸在控制項視窗內拖動時。
UIControlEventTouchDragOutside
當一次觸摸在控制項視窗之外拖動時。
UIControlEventTouchDragEnter
當一次觸摸從控制項視窗之外拖動到內部時。
UIControlEventTouchDragExit
當一次觸摸從控制項視窗內部拖動到外部時。
UIControlEventTouchUpInside
所有在控制項之內觸摸抬起事件。
UIControlEventTouchUpOutside
所有在控制項之外觸摸抬起事件(點觸必須開始與控制項內部才會發送通知)。
UIControlEventTouchCancel
所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者撥打電話打斷。
UIControlEventTouchChanged
當控制項的值發生改變時,發送通知。用於滑塊、分段控制項、以及其他取值的控制項。你可以配置滑塊控制項何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。
UIControlEventEditingDidBegin
當文本控制項中開始編輯時發送通知。
UIControlEventEditingChanged
當文本控制項中的文本被改變時發送通知。
UIControlEventEditingDidEnd
當文本控制項中編輯結束時發送通知。
UIControlEventEditingDidOnExit
當文本控制項內通過按下斷行符號鍵(或等價行為)結束編輯時,發送通知。
UIControlEventAlltouchEvents
通知所有觸摸事件。
UIControlEventAllEditingEvents
通知所有關於文本編輯的事件。
UIControlEventAllEvents
通知所有事件。
執行個體:
[objc] view plaincopy
- 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...");
- }
長按事件
[objc] view plaincopy
- 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];
- }
- }
IOS UIButton用法詳解