一、簡介
<<UIButton(按鈕) : 既能顯示文字挡爵,又能顯示圖片,還能隨時調(diào)整內(nèi)部圖片和文字的位置甚垦,實現(xiàn)了監(jiān)聽用戶的點擊事件茶鹃,在用戶點擊后做出響應(yīng)的功能.在App中UIButton是出現(xiàn)頻率最高的控件之一.
<<繼承關(guān)系:UIButton-->UIControl-->UIView-->UIResponder-->NSObject
<<UIButton的類是一個UIControl子類,它實現(xiàn)了在觸摸屏上的按鈕艰亮。觸摸一個按鈕攔截事件和動作消息發(fā)送到目標對象時闭翩,它的挖掘。設(shè)定的目標和行動方法都繼承自UIControl垃杖。這個類提供了方法來設(shè)置標題男杈,圖像丈屹,按鈕等外觀屬性调俘。通過使用set方法,你可以指定一個不同的外觀為每個按鈕狀態(tài)
格式為
1-->初始化(作用)
ypedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0,? ? ? ? ? ? ? ? ? ? ? ? // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),? // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) __IOS_PROHIBITED __WATCHOS_PROHIBITED, // standard system button without the blurred background view
UIButtonTypeRoundedRect = UIButtonTypeSystem? // Deprecated, use UIButtonTypeSystem instead
};(如果屬性有枚舉類型的話旺垒,這里會有枚舉類型說明)
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; ?(這是具體的例子)
+ (instancetype)buttonWithType:(UIButtonType)buttonType;// UIButton 有自己的初始化方式? ? (這是說明)
二彩库、UIButton的創(chuàng)建(屬性的順序與蘋果API一致)
1-->常規(guī)的 initWithFrame
UIButton?*btn1?=?[[UIButton?alloc]initWithFrame:CGRectMake(10,?10,?80,?44)];
- (instancetype)initWithFrame:(CGRect)frame;// 父類UIView的初始化方法
2-->UIButton 的一個類方法(也可以說是靜態(tài)方法)buttonWithType
typedef enum{
UIButtonTypeCustom?=?0,// ? ?自定義先蒋,無風(fēng)格
UIButtonTypeRoundedRect,// ?白色圓角矩形骇钦,類似偏好設(shè)置表格單元或者地址簿卡片
UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圓圈信息按鈕竞漾,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圓圈信息按鈕
UIButtonTypeContactAdd,//藍色加號(+)按鈕眯搭,可以放在任何文字旁
}?UIButtonType;
UIButton?*btn2?=?[UIButton?buttonWithType:UIButtonTypeCustom];
?UIButton *button = [[UIButton alloc]init];// 和上面一行代碼等價
+ (instancetype)buttonWithType:(UIButtonType)buttonType;//創(chuàng)建并返回一個指定類型的新按鈕窥翩。
注意:
按鈕創(chuàng)建好之后,按鈕的類型是不可以被修改
button.buttonType = UIButtonTypeCustom ; // 錯誤寫法鳞仙,系統(tǒng)會報錯
三寇蚊、UIButton的EdgeInsets內(nèi)邊距設(shè)置
1-->內(nèi)邊距設(shè)置
按鈕內(nèi)容整體向右下分別移動10像素
Button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, -10, -10);
@property(nonatomic) UIEdgeInsets contentEdgeInsets; //默認是UIEdgeInsetsZero。在tvOS 10或稍后棍好,除了定制按鈕之外仗岸,默認值為非0
注意:
UIButton按鈕可以只設(shè)置一個UILabel或者一個UIImageView,還可以同時具有UILabel和UIImageView借笙;如果給按鈕設(shè)置contentEdgeInsets屬性扒怖,就是按鈕的內(nèi)容整體(包含UILabel和UIImageView)進行偏移。
2-->設(shè)置titleLabel的邊界
self.pureButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
@property(nonatomic) UIEdgeInsets titleEdgeInsets; //默認是UIEdgeInsetsZero
3-->設(shè)置imageView的邊界
self.pureButton.imageEdgeInsets= UIEdgeInsetsMake(0, -10, 0,10);
@property(nonatomic) UIEdgeInsets imageEdgeInsets; //默認是UIEdgeInsetsZero
具體效果參看關(guān)于UIButton的UIEdgeInsets屬性
四业稼、UIButton的設(shè)置各種狀態(tài)的效果
1-->設(shè)置高亮狀態(tài) 是否改變陰影
btn. reversesTitleShadowWhenHighlighted = YES; // 默認為NO
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; //默認為NO.如果為YES,當(dāng)按鈕的狀態(tài)進行切換時,文本的影子會反轉(zhuǎn)
2-->設(shè)置高亮狀態(tài) 加深圖片顏色
btn.adjustsImageWhenHighlighted = NO; // 默認為YES 設(shè)置為NO取消效果
@property(nonatomic) BOOL adjustsImageWhenHighlighted; //默認為YES ,如果為YES,當(dāng)按鈕狀態(tài)為高亮?xí)r,圖像變暗.設(shè)置為NO取消效果
3-->設(shè)置失效狀態(tài) 加深圖片顏色
btn.adjustsImageWhenDisabled = NO; // 默認為YES 設(shè)置NO取消效果
@property(nonatomic) BOOL adjustsImageWhenDisabled;//默認為YES.如果為YES,當(dāng)按鈕狀態(tài)為禁用時,圖像變暗
4-->設(shè)置高亮狀態(tài) 按鈕發(fā)光
btn.showsTouchWhenHighlighted = YES; // 默認為NO
@property(nonatomic) BOOL showsTouchWhenHighlighted;//默認為NO.如果為YES, 當(dāng)按鈕狀態(tài)為高亮?xí)r,顯示一個簡單的反饋(類似于光環(huán))
五盗痒、UIButton的繼承父類屬性tintColor
1-->色彩,痕跡,相當(dāng)于是一個描述一個視圖中的線條的顏色
btn.tintColor=[UIColor redColor];
@property(null_resettable, nonatomic,strong) UIColor *tintColor ; //tintColor是通過superview層次結(jié)構(gòu)繼承的
注意:
(1).tintColor是描述線條輪廓的一種顏色,該顏色默認具有傳遞性,默認狀態(tài)下最底部的視圖的tintcolor會一直往上面的視圖傳遞
(2).如果子視圖改變了tintcolor那么將會和父視圖的tintColor不一樣;傳遞鏈從此處斷開
(3).navagation的item的? tintColor和controller自帶的View不是在同一層次上;改變controller的view的tintColor對navagation的tintColor沒有顏色;
(4).由于,tintColor的特性,我們可以對鏤空的圖片(如tabbar的image和 BarButtonItem的image)進行設(shè)置tintColor就可以設(shè)置改變鏤空圖片的顏色
具體效果參看關(guān)于UIButton的tintColor屬性
六、UIButton的按鈕樣式
1-->按鈕的樣式
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0,? ? ? ? ? ? ? ? ? ? ? ? //無樣式
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),? //設(shè)置為系統(tǒng)樣式
UIButtonTypeDetailDisclosure,//藍色小箭頭按鈕低散,主要做詳細說明用
UIButtonTypeInfoLight,//亮色感嘆號(微件(widget)使用的小圓圈信息按鈕)
UIButtonTypeInfoDark,//暗色感嘆號(白色背景下使用的深色圓圈信息按鈕)
UIButtonTypeContactAdd,//十字加號按鈕(藍色加號(+)按鈕)
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) __IOS_PROHIBITED __WATCHOS_PROHIBITED, //沒有模糊背景視圖的標準系統(tǒng)按鈕导梆,TVOS 使用熟丸,IOS和WATCHOS禁止使用
UIButtonTypeRoundedRect = UIButtonTypeSystem? //已過時,用UIButtonTypeSystem代替
};
UIButtonType buttonType=self.btn.buttonType?;
@property(nonatomic,readonly) UIButtonType buttonType;//按鈕的樣式,只讀屬性
七、設(shè)置UIButton的文本或者圖片的展示
1-->設(shè)置button 某狀態(tài)下 的標題
[btn setTitle:@"按鈕" forState:UIControlStateNormal]; // 正常狀態(tài)顯示時候的文本
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; //?設(shè)置UIButton的標題字条摸,forState設(shè)置的是按鈕的當(dāng)前狀態(tài)
2-->設(shè)置button 某狀態(tài)下 的富文本標題
NSMutableAttributedString*attriString?=?[[NSMutableAttributedStringalloc]initWithString:@"123"];
[attriStringaddAttribute:NSForegroundColorAttributeName
value:[UIColorredColor]
range:NSMakeRange(4,2)];
[stateBtnsetAttributedTitle:attriStringforState:UIControlStateNormal];
//正常狀態(tài)顯示時候的富文本
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state; //?設(shè)置UIButton的標題字,forState設(shè)置的是按鈕的當(dāng)前狀態(tài)
注意:
在同一個UIButton上根據(jù)條件先后設(shè)置不帶屬性的title抗悍,即用setTitle來設(shè)置標題亡笑,后來條件變化需要顯示帶屬性的標題(例如下劃線,或者字體多種顏色等等)使用setAttributedTitle祠乃,后來條件再次改變需要顯示不帶屬性的標題
3-->設(shè)置button 某狀態(tài)下 的文本顏色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 正常狀態(tài)顯示時候的顏色梦重,此時為紅色
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state; //設(shè)置標題使用指定的狀態(tài)的顏色。默認黑色
注意:
//設(shè)置文字顏色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//設(shè)置背景顏色--前提是button的type不能為白色圓角矩形亮瓷,因為太大琴拧,button都被白色覆蓋了
[btn setBackgroundColor:[UIColor redColor]];
//設(shè)置按鈕顏色方法
[button setTintColor:[UIColor blueColor]];
4-->設(shè)置某狀態(tài)下標題的陰影顏色
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state; //?默認為nil
5-->設(shè)置某狀態(tài)下的標題圖片
[btn setImage:[UIImage imageNamed:@"btnIcon"] forState:UIControlStateNormal]; // 不會被拉伸,原比例顯示
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // 默認為nil
6-->設(shè)置某狀態(tài)下的背景圖片
[btn setBackgroundImage:[UIImage imageNamed:@"btnBgImg"] forState:UIControlStateNormal]; // 會被拉伸嘱支,充滿整個btn
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;//默認為nil
注意:
[btn setImage:[UIImage imageNamed:@"btnIcon"] forState:UIControlStateNormal]; // 不會被拉伸蚓胸,原比例顯示
[btn setBackgroundImage:[UIImage imageNamed:@"btnBgImg"] forState:UIControlStateNormal]; // 會被拉伸,充滿整個btn
八除师、獲取指定狀態(tài)下的UIButton屬性
1-->獲取指定狀態(tài)下的文本
NSString *title=[btn titleForState:UIControlStateNormal];
- (NSString *)titleForState:(UIControlState)state;//獲取指定狀態(tài)的文本沛膳,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的文字
NSString?*text=?[sender?titleForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的文字
NSString?*text=sender.currentTitle;
2-->獲取指定狀態(tài)下的富文本
NSAttributedString *title=[btn attributedTitleForState:UIControlStateNormal];
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state;//獲取指定狀態(tài)的富文本,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的文字
NSAttributedString?*text=?[sender?attributedTitleForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的文字
NSAttributedString?*text=sender.currentAttributedTitle;
3-->獲取指定狀態(tài)下的標題顏色
UIColor *color=[btn titleColorForState:UIControlStateNormal];
- (nullable UIColor *)titleColorForState:(UIControlState)state;//獲取指定狀態(tài)的標題顏色汛聚,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的標題顏色
UIColor *color=[btn titleColorForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的標題顏色
UIColor *color=sender.currentTitleColor;
4-->獲取指定狀態(tài)下的標題陰影顏色
UIColor *color=[btn titleShadowColorForState:UIControlStateNormal];
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;//獲取指定狀態(tài)的標題陰影顏色锹安,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的標題顏色
UIColor *color=[btn titleShadowColorForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的標題顏色
UIColor *color=sender.currentTitleShadowColor;
5-->獲取指定狀態(tài)下的標題圖片
UIImage *image=[btn imageForState:UIControlStateNormal];
- (nullable UIImage *)imageForState:(UIControlState)state;//獲取指定狀態(tài)的標題圖片,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的標題圖片
UIImage *image=[btn imageForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的標題圖片
UIColor *color=sender.currentImage;
6-->獲取指定狀態(tài)下的背景圖片
UIImage *image=[btn backgroundImageForState:UIControlStateNormal];
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;//獲取指定狀態(tài)的背景圖片,不一定是當(dāng)前狀態(tài)
//獲取按鈕指定狀態(tài)下的背景圖片
UIImage *image=[btn ?backgroundImageForState:UIControlStateNormal];
//獲取當(dāng)前狀態(tài)下的背景圖片
UIColor *color=sender.currentBackgroundImage;
九叹哭、獲取當(dāng)前狀態(tài)下的UIButton屬性
1-->獲取當(dāng)前狀態(tài)下的文本
NSString?*text=sender.currentTitle;
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;//只讀屬性
2-->獲取當(dāng)前狀態(tài)下的富文本
NSAttributedString?*text=sender.currentAttributedTitle;
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle;?//只讀屬性
3-->獲取當(dāng)前狀態(tài)下的標題顏色
UIColor *color=sender.currentTitleColor;
@property(nonatomic,readonly,strong) UIColor *currentTitleColor;//只讀屬性
4-->獲取當(dāng)前狀態(tài)下的標題陰影顏色
UIColor *color=sender.currentTitleShadowColor;
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;//只讀屬性
5-->獲取當(dāng)前狀態(tài)下的標題圖片
UIImage *currentImage=sender.currentImage;
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage;?//只讀屬性
6-->獲取當(dāng)前狀態(tài)下的背景圖片
UIColor *color=sender.currentBackgroundImage;
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage;?//只讀屬性
十忍宋、UIButton的titleLabel屬性(只讀)
1-->按鈕中顯示的label顯示的內(nèi)容,此屬性與currentTitle有很大不同
button.titleLabel.font = [UIFont fontWithName:@"STHeitiTC-Light" size:18];
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0)//只讀屬性
只讀屬性风罩,不要用以下方法賦值:
button.titleLabel.text=@"只讀";
詳見
iOS 中UIButton的 settitle 和 titlelabel的使用誤區(qū)
十一讶踪、UIButton的imageView屬性(只讀)
1-->按鈕中顯示的imageView ,此屬性與currentImage有很大不同
UIImage *imageView=button.imageView;
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0);
只讀屬性泊交,不要用以下方法賦值:
button.imageView.image = [UIImage imageNamed:[UIImage imageNamed:@"1"]];
詳見
UIButton設(shè)置Image和Label時乳讥,setImage和UIButton.ImageView.image的區(qū)別
十二、獲取UIButton的屬性邊界
你可以通過子類化按鈕來定制屬于你自己的按鈕類廓俭。在子類化的時候你可以重載下面這些方法云石,這些方法返回CGRect結(jié)構(gòu),指明了按鈕每一組成部分的邊界研乒。
注意:不要直接調(diào)用這些方法汹忠, 這些方法是你寫給系統(tǒng)調(diào)用的。
1-->指定背景邊界(請勿直接調(diào)用)
- (CGRect)backgroundRectForBounds(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);?
?}
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
2-->指定內(nèi)容邊界(請勿直接調(diào)用)
- (CGRect)contentRectForBounds(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);
}
- (CGRect)contentRectForBounds:(CGRect)bounds;
3-->指定文字標題邊界(請勿直接調(diào)用)
- (CGRect)titleRectForContentRect(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);
}
- (CGRect)titleRectForContentRect:(CGRect)bounds;
4-->titleRectForContentRect(請勿直接調(diào)用)
- (CGRect)imageRectForContentRect(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);
}
- (CGRect)imageRectForContentRect:(CGRect)bounds;
十三雹熬、UIButton的廢棄屬性
@property(nonatomic,strong) UIFont *font NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
@property(nonatomic)? ? ? ? NSLineBreakMode lineBreakMode? ? NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
@property(nonatomic)? ? ? ? CGSize? ? ? ? ? titleShadowOffset NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
十四宽菜、UIButton的狀態(tài)
1. normal(普通狀態(tài))
默認情況(Default)
對應(yīng)的枚舉常量:UIControlStateNormal
除開UIControlStateHighlighted、UIControlStateDisabled竿报、UIControlStateSelected以外的其他情況铅乡,都是normal狀態(tài)
這種狀態(tài)下的按鈕【可以】接收點擊事件
如果前后連著設(shè)置按鈕同時處于多種狀態(tài), 則表現(xiàn)出來的也是 normal 狀態(tài), 除去如果有 設(shè)置為 enabled = NO; 則會進入UIControlStateDisabled狀態(tài)(包括顏色), 不能點擊
//下面兩種雜交在一起(就不是 normal 后面三種 ), 會顯示為 Normal 狀態(tài)的顏色,
//但是 設(shè)置了 Enabled == NO, 所以這里也是不能點擊的,
self.button.selected=YES;self.button.enabled=NO;
2. highlighted(高亮狀態(tài))
對應(yīng)的枚舉常量:UIControlStateHighlighted
【當(dāng)按住按鈕不松開】或者【highlighted = YES】時就能達到這種狀態(tài)
這種狀態(tài)下的按鈕【可以】接收點擊事件
3.selected (選中狀態(tài))
對應(yīng)的枚舉常量:UIControlStateSelected
【button.selected = YES】時就能達到這種狀態(tài)
這種狀態(tài)下的按鈕【可以】接收點擊事件
4. disabled(失效狀態(tài),不可用狀態(tài))
如果enabled屬性為NO烈菌,就是處于disable狀態(tài)阵幸,代表按鈕不可以被點擊
對應(yīng)的枚舉常量:UIControlStateDisabled
【button.enabled = NO】時就能達到這種狀態(tài)
這種狀態(tài)下的按鈕【無法】接收點擊事件
5. 讓按鈕無法點擊的2種方法
button.enabled = NO;
【會】進入UIControlStateDisabled狀態(tài)
button.userInteractionEnabled = NO;
【不會】進入UIControlStateDisabled狀態(tài),繼續(xù)保持當(dāng)前狀態(tài)
十五芽世、UIButton的事件處理
UIButton繼承了UIControl挚赊,所以可以使用UIControl提供的事件處理方法,常用的有如下:
1- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents //給目標添加事件济瓢,使用該方法實現(xiàn)Target-action模式
2- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents //移除目標上的事件
1-->添加事件
typedef ?NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 0,? ??
?單點觸摸按下事件:用戶點觸屏幕荠割,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 1,? ?
??多點觸摸按下事件旺矾,點觸計數(shù)大于1:用戶按下第二蔑鹦、三、或第四根手指的時候宠漩。
UIControlEventTouchDragInside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 2,
當(dāng)一次觸摸在控件窗口內(nèi)拖動時举反。
UIControlEventTouchDragOutside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 3,
當(dāng)一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 4,
當(dāng)一次觸摸從控件窗口之外拖動到內(nèi)部時扒吁。
UIControlEventTouchDragExit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 5,
當(dāng)一次觸摸從控件窗口內(nèi)部拖動到外部時。
UIControlEventTouchUpInside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 6,
所有在控件之內(nèi)觸摸抬起事件。
UIControlEventTouchUpOutside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 7,
所有在控件之外觸摸抬起事件(點觸必須開始與控件內(nèi)部才會發(fā)送通知)雕崩。
UIControlEventTouchCancel? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 8,
所有觸摸取消事件魁索,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷盼铁。
UIControlEventValueChanged? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 12, ?
當(dāng)控件的值發(fā)生改變時粗蔚,發(fā)送通知。用于滑塊饶火、分段控件鹏控、以及其他取值的控件。你可以配置滑塊控件何時發(fā)送通知肤寝,在滑塊被放下時發(fā)送当辐,或者在被拖動時發(fā)送。UIControlEventEditingDidBegin? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 16, ? ?
當(dāng)文本控件中開始編輯時發(fā)送通知鲤看。
UIControlEventEditingChanged? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 17,
當(dāng)文本控件中的文本被改變時發(fā)送通知缘揪。
UIControlEventEditingDidEnd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 18,
當(dāng)文本控件中編輯結(jié)束時發(fā)送通知。
UIControlEventEditingDidEndOnExit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 19, ? ?
當(dāng)文本控件內(nèi)通過按下回車鍵(或等價行為)結(jié)束編輯時义桂,發(fā)送通知找筝。
UIControlEventAllTouchEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x00000FFF, ?
通知所有觸摸事件。
UIControlEventAllEditingEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x000F0000, ?
通知所有關(guān)于文本編輯的事件慷吊。
UIControlEventApplicationReserved? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x0F000000,?
預(yù)留給application(應(yīng)用)使用的事件袖裕。
UIControlEventSystemReserved? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0xF0000000, ?
預(yù)留給internal framework(內(nèi)部框架)使用的事件。
UIControlEventAllEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0xFFFFFFFF
通知所有事件溉瓶。
};
[button addTarget:self action:@selector(text:) forControlEvents:UIControlEventTouchUpInside];
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
1陆赋、self 指目標對象為當(dāng)前對象
2、action 即 在目標對象上的點擊方法嚷闭;
action方法有以下幾種形式:
- (void)doSomething;
- (void)doSomething:(id)sender;
- (IBAction)doSomething:(id)sender;
- (IBAction)doSomething:(UIButton *) sender;
3攒岛、何時調(diào)用該方法,UIControlEventTouchUpInside即單擊時胞锰。
UIButton的幾種觸發(fā)條件
1灾锯、UIControlEventTouchDown
指鼠標左鍵按下(注:只是“按下”)的動作
2、UIControlEventTouchDownRepeat
指鼠標左鍵連續(xù)多次重復(fù)按下(注:只是“按下”)的動作嗅榕,比如顺饮,鼠標連續(xù)雙擊、三擊凌那、……兼雄、多次連擊。
說明:多次重復(fù)按下時帽蝶,事件序列是這樣的:
UIControlEventTouchDown?->
(UIControlEventTouchUpInside)?->
UIControlEventTouchDown?->
UIControlEventTouchDownRepeat?->
(UIControlEventTouchUpInside)?->
UIControlEventTouchDown?->
UIControlEventTouchDownRepeat?->
(UIControlEventTouchUpInside)?->
......
除了第一次按下外赦肋,后面每次摁下都是一個UIControlEventTouchDown事件,然后緊跟一個UIControlEventTouchDownRepeat事件。
3佃乘、UIControlEventTouchDragInside
指按下鼠標囱井,然后在控件邊界范圍內(nèi)拖動。
4趣避、UIControlEventTouchDragOutside
與UIControlEventTouchDragInside不同的是庞呕,拖動時,鼠標位于控件邊界范圍之外程帕。
但首先得有個UIControlEventTouchDown事件住练,然后接一個UIControlEventTouchDragInside事件,再接一個UIControlEventTouchDragExit事件愁拭,這時讲逛,鼠標已經(jīng)位于控件外了,繼續(xù)拖動就是UIControlEventTouchDragOutside事件了敛苇。
具體操作是:在控件里面按下鼠標妆绞,然后拖動到控件之外。
5枫攀、UIControlEventTouchDragEnter
指拖動動作中括饶,從控件邊界外到內(nèi)時產(chǎn)生的事件。
6来涨、UIControlEventTouchDragExit
指拖動動作中图焰,從控件邊界內(nèi)到外時產(chǎn)生的事件。
7蹦掐、UIControlEventTouchUpInside
指鼠標在控件范圍內(nèi)抬起技羔,前提先得按下,即UIControlEventTouchDown或UIControlEventTouchDownRepeat事件卧抗。
8藤滥、UIControlEventTouchUpOutside
指鼠標在控件邊界范圍外抬起,前提先得按下社裆,然后拖動到控件外拙绊,即
UIControlEventTouchDown?->
UIControlEventTouchDragInside(n?個)?->
UIControlEventTouchDragExit?->
UIControlEventTouchDragOutside(n?個)
時間序列,再然后就是抬起鼠標泳秀,產(chǎn)生UIControlEventTouchUpOutside事件
2-->添加事件
[self.camaraBtn removeTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];//與以上同理
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents //移除目標上的事件