一、簡介
<<UIButton(按鈕) : 既能顯示文字,又能顯示圖片,還能隨時調整內部圖片和文字的位置,實現了監聽用戶的點擊事件,在用戶點擊后做出響應的功能.在App中UIButton是出現頻率最高的控件之一.
<<繼承關系:UIButton-->UIControl-->UIView-->UIResponder-->NSObject
<<UIButton的類是一個UIControl子類,它實現了在觸摸屏上的按鈕。觸摸一個按鈕攔截事件和動作消息發送到目標對象時,它的挖掘。設定的目標和行動方法都繼承自UIControl。這個類提供了方法來設置標題,圖像,按鈕等外觀屬性。通過使用set方法,你可以指定一個不同的外觀為每個按鈕狀態
格式為
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的創建(屬性的順序與蘋果API一致)
1-->常規的 initWithFrame
UIButton?*btn1?=?[[UIButton?alloc]initWithFrame:CGRectMake(10,?10,?80,?44)];
- (instancetype)initWithFrame:(CGRect)frame;// 父類UIView的初始化方法
2-->UIButton 的一個類方法(也可以說是靜態方法)buttonWithType
typedef enum{
UIButtonTypeCustom?=?0,// ? ?自定義,無風格
UIButtonTypeRoundedRect,// ?白色圓角矩形,類似偏好設置表格單元或者地址簿卡片
UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圓圈信息按鈕,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圓圈信息按鈕
UIButtonTypeContactAdd,//藍色加號(+)按鈕,可以放在任何文字旁
}?UIButtonType;
UIButton?*btn2?=?[UIButton?buttonWithType:UIButtonTypeCustom];
?UIButton *button = [[UIButton alloc]init];// 和上面一行代碼等價
+ (instancetype)buttonWithType:(UIButtonType)buttonType;//創建并返回一個指定類型的新按鈕。
注意:
按鈕創建好之后,按鈕的類型是不可以被修改
button.buttonType = UIButtonTypeCustom ; // 錯誤寫法,系統會報錯
三、UIButton的EdgeInsets內邊距設置
1-->內邊距設置
按鈕內容整體向右下分別移動10像素
Button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, -10, -10);
@property(nonatomic) UIEdgeInsets contentEdgeInsets; //默認是UIEdgeInsetsZero。在tvOS 10或稍后,除了定制按鈕之外,默認值為非0
注意:
UIButton按鈕可以只設置一個UILabel或者一個UIImageView,還可以同時具有UILabel和UIImageView;如果給按鈕設置contentEdgeInsets屬性,就是按鈕的內容整體(包含UILabel和UIImageView)進行偏移。
2-->設置titleLabel的邊界
self.pureButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
@property(nonatomic) UIEdgeInsets titleEdgeInsets; //默認是UIEdgeInsetsZero
3-->設置imageView的邊界
self.pureButton.imageEdgeInsets= UIEdgeInsetsMake(0, -10, 0,10);
@property(nonatomic) UIEdgeInsets imageEdgeInsets; //默認是UIEdgeInsetsZero
具體效果參看關于UIButton的UIEdgeInsets屬性
四、UIButton的設置各種狀態的效果
1-->設置高亮狀態 是否改變陰影
btn. reversesTitleShadowWhenHighlighted = YES; // 默認為NO
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; //默認為NO.如果為YES,當按鈕的狀態進行切換時,文本的影子會反轉
2-->設置高亮狀態 加深圖片顏色
btn.adjustsImageWhenHighlighted = NO; // 默認為YES 設置為NO取消效果
@property(nonatomic) BOOL adjustsImageWhenHighlighted; //默認為YES ,如果為YES,當按鈕狀態為高亮時,圖像變暗.設置為NO取消效果
3-->設置失效狀態 加深圖片顏色
btn.adjustsImageWhenDisabled = NO; // 默認為YES 設置NO取消效果
@property(nonatomic) BOOL adjustsImageWhenDisabled;//默認為YES.如果為YES,當按鈕狀態為禁用時,圖像變暗
4-->設置高亮狀態 按鈕發光
btn.showsTouchWhenHighlighted = YES; // 默認為NO
@property(nonatomic) BOOL showsTouchWhenHighlighted;//默認為NO.如果為YES, 當按鈕狀態為高亮時,顯示一個簡單的反饋(類似于光環)
五、UIButton的繼承父類屬性tintColor
1-->色彩,痕跡,相當于是一個描述一個視圖中的線條的顏色
btn.tintColor=[UIColor redColor];
@property(null_resettable, nonatomic,strong) UIColor *tintColor ; //tintColor是通過superview層次結構繼承的
注意:
(1).tintColor是描述線條輪廓的一種顏色,該顏色默認具有傳遞性,默認狀態下最底部的視圖的tintcolor會一直往上面的視圖傳遞
(2).如果子視圖改變了tintcolor那么將會和父視圖的tintColor不一樣;傳遞鏈從此處斷開
(3).navagation的item的? tintColor和controller自帶的View不是在同一層次上;改變controller的view的tintColor對navagation的tintColor沒有顏色;
(4).由于,tintColor的特性,我們可以對鏤空的圖片(如tabbar的image和 BarButtonItem的image)進行設置tintColor就可以設置改變鏤空圖片的顏色
具體效果參看關于UIButton的tintColor屬性
六、UIButton的按鈕樣式
1-->按鈕的樣式
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0,? ? ? ? ? ? ? ? ? ? ? ? //無樣式
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),? //設置為系統樣式
UIButtonTypeDetailDisclosure,//藍色小箭頭按鈕,主要做詳細說明用
UIButtonTypeInfoLight,//亮色感嘆號(微件(widget)使用的小圓圈信息按鈕)
UIButtonTypeInfoDark,//暗色感嘆號(白色背景下使用的深色圓圈信息按鈕)
UIButtonTypeContactAdd,//十字加號按鈕(藍色加號(+)按鈕)
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) __IOS_PROHIBITED __WATCHOS_PROHIBITED, //沒有模糊背景視圖的標準系統按鈕,TVOS 使用,IOS和WATCHOS禁止使用
UIButtonTypeRoundedRect = UIButtonTypeSystem? //已過時,用UIButtonTypeSystem代替
};
UIButtonType buttonType=self.btn.buttonType?;
@property(nonatomic,readonly) UIButtonType buttonType;//按鈕的樣式,只讀屬性
七、設置UIButton的文本或者圖片的展示
1-->設置button 某狀態下 的標題
[btn setTitle:@"按鈕" forState:UIControlStateNormal]; // 正常狀態顯示時候的文本
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; //?設置UIButton的標題字,forState設置的是按鈕的當前狀態
2-->設置button 某狀態下 的富文本標題
NSMutableAttributedString*attriString?=?[[NSMutableAttributedStringalloc]initWithString:@"123"];
[attriStringaddAttribute:NSForegroundColorAttributeName
value:[UIColorredColor]
range:NSMakeRange(4,2)];
[stateBtnsetAttributedTitle:attriStringforState:UIControlStateNormal];
//正常狀態顯示時候的富文本
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state; //?設置UIButton的標題字,forState設置的是按鈕的當前狀態
注意:
在同一個UIButton上根據條件先后設置不帶屬性的title,即用setTitle來設置標題,后來條件變化需要顯示帶屬性的標題(例如下劃線,或者字體多種顏色等等)使用setAttributedTitle,后來條件再次改變需要顯示不帶屬性的標題
3-->設置button 某狀態下 的文本顏色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 正常狀態顯示時候的顏色,此時為紅色
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state; //設置標題使用指定的狀態的顏色。默認黑色
注意:
//設置文字顏色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//設置背景顏色--前提是button的type不能為白色圓角矩形,因為太大,button都被白色覆蓋了
[btn setBackgroundColor:[UIColor redColor]];
//設置按鈕顏色方法
[button setTintColor:[UIColor blueColor]];
4-->設置某狀態下標題的陰影顏色
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state; //?默認為nil
5-->設置某狀態下的標題圖片
[btn setImage:[UIImage imageNamed:@"btnIcon"] forState:UIControlStateNormal]; // 不會被拉伸,原比例顯示
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // 默認為nil
6-->設置某狀態下的背景圖片
[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
八、獲取指定狀態下的UIButton屬性
1-->獲取指定狀態下的文本
NSString *title=[btn titleForState:UIControlStateNormal];
- (NSString *)titleForState:(UIControlState)state;//獲取指定狀態的文本,不一定是當前狀態
//獲取按鈕指定狀態下的文字
NSString?*text=?[sender?titleForState:UIControlStateNormal];
//獲取當前狀態下的文字
NSString?*text=sender.currentTitle;
2-->獲取指定狀態下的富文本
NSAttributedString *title=[btn attributedTitleForState:UIControlStateNormal];
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state;//獲取指定狀態的富文本,不一定是當前狀態
//獲取按鈕指定狀態下的文字
NSAttributedString?*text=?[sender?attributedTitleForState:UIControlStateNormal];
//獲取當前狀態下的文字
NSAttributedString?*text=sender.currentAttributedTitle;
3-->獲取指定狀態下的標題顏色
UIColor *color=[btn titleColorForState:UIControlStateNormal];
- (nullable UIColor *)titleColorForState:(UIControlState)state;//獲取指定狀態的標題顏色,不一定是當前狀態
//獲取按鈕指定狀態下的標題顏色
UIColor *color=[btn titleColorForState:UIControlStateNormal];
//獲取當前狀態下的標題顏色
UIColor *color=sender.currentTitleColor;
4-->獲取指定狀態下的標題陰影顏色
UIColor *color=[btn titleShadowColorForState:UIControlStateNormal];
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;//獲取指定狀態的標題陰影顏色,不一定是當前狀態
//獲取按鈕指定狀態下的標題顏色
UIColor *color=[btn titleShadowColorForState:UIControlStateNormal];
//獲取當前狀態下的標題顏色
UIColor *color=sender.currentTitleShadowColor;
5-->獲取指定狀態下的標題圖片
UIImage *image=[btn imageForState:UIControlStateNormal];
- (nullable UIImage *)imageForState:(UIControlState)state;//獲取指定狀態的標題圖片,不一定是當前狀態
//獲取按鈕指定狀態下的標題圖片
UIImage *image=[btn imageForState:UIControlStateNormal];
//獲取當前狀態下的標題圖片
UIColor *color=sender.currentImage;
6-->獲取指定狀態下的背景圖片
UIImage *image=[btn backgroundImageForState:UIControlStateNormal];
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;//獲取指定狀態的背景圖片,不一定是當前狀態
//獲取按鈕指定狀態下的背景圖片
UIImage *image=[btn ?backgroundImageForState:UIControlStateNormal];
//獲取當前狀態下的背景圖片
UIColor *color=sender.currentBackgroundImage;
九、獲取當前狀態下的UIButton屬性
1-->獲取當前狀態下的文本
NSString?*text=sender.currentTitle;
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;//只讀屬性
2-->獲取當前狀態下的富文本
NSAttributedString?*text=sender.currentAttributedTitle;
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle;?//只讀屬性
3-->獲取當前狀態下的標題顏色
UIColor *color=sender.currentTitleColor;
@property(nonatomic,readonly,strong) UIColor *currentTitleColor;//只讀屬性
4-->獲取當前狀態下的標題陰影顏色
UIColor *color=sender.currentTitleShadowColor;
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;//只讀屬性
5-->獲取當前狀態下的標題圖片
UIImage *currentImage=sender.currentImage;
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage;?//只讀屬性
6-->獲取當前狀態下的背景圖片
UIColor *color=sender.currentBackgroundImage;
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage;?//只讀屬性
十、UIButton的titleLabel屬性(只讀)
1-->按鈕中顯示的label顯示的內容,此屬性與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的使用誤區
十一、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設置Image和Label時,setImage和UIButton.ImageView.image的區別
十二、獲取UIButton的屬性邊界
你可以通過子類化按鈕來定制屬于你自己的按鈕類。在子類化的時候你可以重載下面這些方法,這些方法返回CGRect結構,指明了按鈕每一組成部分的邊界。
注意:不要直接調用這些方法, 這些方法是你寫給系統調用的。
1-->指定背景邊界(請勿直接調用)
- (CGRect)backgroundRectForBounds(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);?
?}
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
2-->指定內容邊界(請勿直接調用)
- (CGRect)contentRectForBounds(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);
}
- (CGRect)contentRectForBounds:(CGRect)bounds;
3-->指定文字標題邊界(請勿直接調用)
- (CGRect)titleRectForContentRect(CGRect)bounds{
return CGRectMake(0.0,0.0,44,44);
}
- (CGRect)titleRectForContentRect:(CGRect)bounds;
4-->titleRectForContentRect(請勿直接調用)
- (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的狀態
1. normal(普通狀態)
默認情況(Default)
對應的枚舉常量:UIControlStateNormal
除開UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情況,都是normal狀態
這種狀態下的按鈕【可以】接收點擊事件
如果前后連著設置按鈕同時處于多種狀態, 則表現出來的也是 normal 狀態, 除去如果有 設置為 enabled = NO; 則會進入UIControlStateDisabled狀態(包括顏色), 不能點擊
//下面兩種雜交在一起(就不是 normal 后面三種 ), 會顯示為 Normal 狀態的顏色,
//但是 設置了 Enabled == NO, 所以這里也是不能點擊的,
self.button.selected=YES;self.button.enabled=NO;
2. highlighted(高亮狀態)
對應的枚舉常量:UIControlStateHighlighted
【當按住按鈕不松開】或者【highlighted = YES】時就能達到這種狀態
這種狀態下的按鈕【可以】接收點擊事件
3.selected (選中狀態)
對應的枚舉常量:UIControlStateSelected
【button.selected = YES】時就能達到這種狀態
這種狀態下的按鈕【可以】接收點擊事件
4. disabled(失效狀態,不可用狀態)
如果enabled屬性為NO,就是處于disable狀態,代表按鈕不可以被點擊
對應的枚舉常量:UIControlStateDisabled
【button.enabled = NO】時就能達到這種狀態
這種狀態下的按鈕【無法】接收點擊事件
5. 讓按鈕無法點擊的2種方法
button.enabled = NO;
【會】進入UIControlStateDisabled狀態
button.userInteractionEnabled = NO;
【不會】進入UIControlStateDisabled狀態,繼續保持當前狀態
十五、UIButton的事件處理
UIButton繼承了UIControl,所以可以使用UIControl提供的事件處理方法,常用的有如下:
1- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents //給目標添加事件,使用該方法實現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,? ?
??多點觸摸按下事件,點觸計數大于1:用戶按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 2,
當一次觸摸在控件窗口內拖動時。
UIControlEventTouchDragOutside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 3,
當一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 4,
當一次觸摸從控件窗口之外拖動到內部時。
UIControlEventTouchDragExit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 5,
當一次觸摸從控件窗口內部拖動到外部時。
UIControlEventTouchUpInside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 6,
所有在控件之內觸摸抬起事件。
UIControlEventTouchUpOutside? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 7,
所有在控件之外觸摸抬起事件(點觸必須開始與控件內部才會發送通知)。
UIControlEventTouchCancel? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 <<? 8,
所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。
UIControlEventValueChanged? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 12, ?
當控件的值發生改變時,發送通知。用于滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。UIControlEventEditingDidBegin? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 16, ? ?
當文本控件中開始編輯時發送通知。
UIControlEventEditingChanged? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 17,
當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 18,
當文本控件中編輯結束時發送通知。
UIControlEventEditingDidEndOnExit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1 << 19, ? ?
當文本控件內通過按下回車鍵(或等價行為)結束編輯時,發送通知。
UIControlEventAllTouchEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x00000FFF, ?
通知所有觸摸事件。
UIControlEventAllEditingEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x000F0000, ?
通知所有關于文本編輯的事件。
UIControlEventApplicationReserved? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x0F000000,?
預留給application(應用)使用的事件。
UIControlEventSystemReserved? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0xF0000000, ?
預留給internal framework(內部框架)使用的事件。
UIControlEventAllEvents? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0xFFFFFFFF
通知所有事件。
};
[button addTarget:self action:@selector(text:) forControlEvents:UIControlEventTouchUpInside];
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
1、self 指目標對象為當前對象
2、action 即 在目標對象上的點擊方法;
action方法有以下幾種形式:
- (void)doSomething;
- (void)doSomething:(id)sender;
- (IBAction)doSomething:(id)sender;
- (IBAction)doSomething:(UIButton *) sender;
3、何時調用該方法,UIControlEventTouchUpInside即單擊時。
UIButton的幾種觸發條件
1、UIControlEventTouchDown
指鼠標左鍵按下(注:只是“按下”)的動作
2、UIControlEventTouchDownRepeat
指鼠標左鍵連續多次重復按下(注:只是“按下”)的動作,比如,鼠標連續雙擊、三擊、……、多次連擊。
說明:多次重復按下時,事件序列是這樣的:
UIControlEventTouchDown?->
(UIControlEventTouchUpInside)?->
UIControlEventTouchDown?->
UIControlEventTouchDownRepeat?->
(UIControlEventTouchUpInside)?->
UIControlEventTouchDown?->
UIControlEventTouchDownRepeat?->
(UIControlEventTouchUpInside)?->
......
除了第一次按下外,后面每次摁下都是一個UIControlEventTouchDown事件,然后緊跟一個UIControlEventTouchDownRepeat事件。
3、UIControlEventTouchDragInside
指按下鼠標,然后在控件邊界范圍內拖動。
4、UIControlEventTouchDragOutside
與UIControlEventTouchDragInside不同的是,拖動時,鼠標位于控件邊界范圍之外。
但首先得有個UIControlEventTouchDown事件,然后接一個UIControlEventTouchDragInside事件,再接一個UIControlEventTouchDragExit事件,這時,鼠標已經位于控件外了,繼續拖動就是UIControlEventTouchDragOutside事件了。
具體操作是:在控件里面按下鼠標,然后拖動到控件之外。
5、UIControlEventTouchDragEnter
指拖動動作中,從控件邊界外到內時產生的事件。
6、UIControlEventTouchDragExit
指拖動動作中,從控件邊界內到外時產生的事件。
7、UIControlEventTouchUpInside
指鼠標在控件范圍內抬起,前提先得按下,即UIControlEventTouchDown或UIControlEventTouchDownRepeat事件。
8、UIControlEventTouchUpOutside
指鼠標在控件邊界范圍外抬起,前提先得按下,然后拖動到控件外,即
UIControlEventTouchDown?->
UIControlEventTouchDragInside(n?個)?->
UIControlEventTouchDragExit?->
UIControlEventTouchDragOutside(n?個)
時間序列,再然后就是抬起鼠標,產生UIControlEventTouchUpOutside事件
2-->添加事件
[self.camaraBtn removeTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];//與以上同理
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents //移除目標上的事件