-
所有的手勢操作都繼承于UIGestureRecognizer,這個類本身是一個虛類,不能直接使用。這個類中定義了這幾種手勢共有的一些屬性和方法(下表僅列出常用屬性和方法):
名稱 | 說明 |
---|---|
屬性 | |
@property(nonatomic,readonly) UIGestureRecognizerState state; | 手勢狀態(tài) |
@property(nonatomic, getter=isEnabled) BOOL enabled; | 手勢是否可用 |
@property(nonatomic,readonly) UIView *view; | 觸發(fā)手勢的視圖(一般在觸摸執(zhí)行操作中我們可以通過此屬性獲得觸摸視圖進行操作) |
@property(nonatomic) BOOL delaysTouchesBegan; | 手勢識別失敗前不執(zhí)行觸摸開始事件,默認為NO;如果為YES,那么成功識別則不執(zhí)行觸摸開始事件,失敗則執(zhí)行觸摸開始事件;如果為NO,則不管成功與否都執(zhí)行觸摸開始事件; |
方法 | |
- (void)addTarget:(id)target action:(SEL)action; | 添加觸摸執(zhí)行事件 |
- (void)removeTarget:(id)target action:(SEL)action; | 移除觸摸執(zhí)行事件 |
- (NSUInteger)numberOfTouches; | 觸摸點的個數(同時觸摸的手指數) |
- (CGPoint)locationInView:(UIView*)view; | 在指定視圖中的相對位置 |
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view; | 觸摸點相對于指定視圖的位置 |
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; | 指定一個手勢需要另一個手勢執(zhí)行失敗才會執(zhí)行 |
代理方法 | UIGestureRecognizerDelegate |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; | 一個控件的手勢識別后是否阻斷手勢識別繼續(xù)向下傳播,默認返回NO;如果為YES,響應者鏈上層對象觸發(fā)手勢識別后,如果下層對象也添加了手勢并成功識別也會繼續(xù)執(zhí)行,否則上層對象識別后則不再繼續(xù)傳播; |
-
我們能直接使用的是UIGestureRecognizer類的子類,它的子類有:
手勢 | 說明 |
---|---|
UITapGestureRecognizer | 點擊手勢 |
UIPinchGestureRecognizer | 捏合手勢 |
UIPanGestureRecognizer | 拖動手勢 |
UISwipeGestureRecognizer | 輕掃手勢,支持四個方向,但多個方向需要添加多個手勢 |
UIRotationGestureRecognizer | 旋轉手勢 |
UILongPressGestureRecognizer | 長按手勢 |
-
手勢的狀態(tài)
- 在iOS中將手勢狀態(tài)分為如下幾種:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // 尚未識別是何種手勢操作(但可能已經觸發(fā)了觸摸事件),默認狀態(tài)
UIGestureRecognizerStateBegan, // 手勢已經開始,此時已經被識別,但是這個過程中可能發(fā)生變化,手勢操作尚未完成
UIGestureRecognizerStateChanged, // 手勢狀態(tài)發(fā)生轉變
UIGestureRecognizerStateEnded, // 手勢識別操作完成(此時已經松開手指)
UIGestureRecognizerStateCancelled, // 手勢被取消,恢復到默認狀態(tài)
UIGestureRecognizerStateFailed, // t手勢識別失敗,恢復到默認狀態(tài)
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手勢識別完成,同UIGestureRecognizerStateEnded
};
- 在iOS中添加手勢比較簡單,可以歸納為以下幾個步驟:
- 創(chuàng)建對應的手勢對象;
- 設置手勢識別屬性【可選】;
- 附加手勢到指定的對象;
- 編寫手勢操作方法;
參考【iOS_成才錄】的【iOS --蘋果自帶的UIMenuController功能擴展】自定義了Label,給Label加上UIMenuController菜單欄,這里就用到了點擊手勢。
#import "QSLabel.h"
@implementation QSLabel
/**
* 初始化
*/
- (void)awakeFromNib
{
[super awakeFromNib];
// 添加手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(theLabelClick)];
[self addGestureRecognizer:tap];
[tap setNumberOfTapsRequired:2];
// 這里一定要設置為YES,不然接收不到手勢事件
self.userInteractionEnabled = YES;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 添加手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(theLabelClick)];
[self addGestureRecognizer:tap];
[tap setNumberOfTapsRequired:2];
self.userInteractionEnabled = YES;
}
return self;
}
/**
* 點擊label
*/
- (void)theLabelClick
{
// 成為第一響應者
[self becomeFirstResponder];
// 創(chuàng)建菜單
UIMenuController *menuController = [UIMenuController sharedMenuController];
// 設置菜單顯示的內容
menuController.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復制" action:@selector(myCopy:)],
[[UIMenuItem alloc] initWithTitle:@"剪切" action:@selector(myCut:)],
[[UIMenuItem alloc] initWithTitle:@"粘貼" action:@selector(myPaste:)]
];
// 設置菜單顯示的位置
[menuController setTargetRect:self.bounds inView:self];
// 顯示菜單
[menuController setMenuVisible:YES animated:YES];
}
#pragma mark - UIMenuController相關
/**
* 讓label具備第一響應者的資格
*/
- (BOOL)canBecomeFirstResponder
{
return YES;
}
/**
* 通過第一響應者的這個方法告訴UIMenuController可以顯示什么內容
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ((action == @selector(myCopy:) && self.text)
|| (action == @selector(myCut:) && self.text)
|| (action == @selector(myPaste:))) {
return YES;
}
return NO;
}
#pragma mark - 監(jiān)聽MenuItem的點擊事件
/**
* 復制
*/
- (void)myCopy:(UIMenuController *)menu
{
// 將文字存儲到剪貼板
[UIPasteboard generalPasteboard].string = self.text;
}
/**
* 剪切
*/
- (void)myCut:(UIMenuController *)menu
{
// 將文字存儲到剪貼板
[UIPasteboard generalPasteboard].string = self.text;
// 刪除文字
self.text = nil;
}
/**
* 粘貼
*/
- (void)myPaste:(UIMenuController *)menu
{
self.text = [UIPasteboard generalPasteboard].string;
}
@end
-
手勢沖突
同一個控件添加多個手勢的時候,有時就會產生沖突,例如:輕掃和拖動;長按和拖動等
在iOS中,如果一個手勢A的識別部分是另一個手勢B的子部分時,默認情況下A就會先識別,B就無法識別了。要解決這個沖突可以利用- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;方法來完成。正是前面表格中UIGestureRecognizer的最后一個方法,這個方法可以指定某個手勢執(zhí)行的前提是另一個手勢失敗才會識別執(zhí)行。也就是說如果我們指定拖動手勢的執(zhí)行前提為輕掃手勢失敗才執(zhí)行,這樣一來當我們手指輕輕滑動時,系統(tǒng)會優(yōu)先考慮輕掃手勢,如果最后發(fā)現該操作不是輕掃,那么才會執(zhí)行拖動。
-
兩個不同的控件上的手勢同時執(zhí)行
有時,同一個手勢我們需要多個控件同時執(zhí)行。
我們知道在iOS的觸摸事件中,事件觸發(fā)是根據響應者鏈進行的,上層觸摸事件執(zhí)行后就不再向下傳播。默認情況下手勢也是類似的,先識別的手勢會阻斷手勢識別操作繼續(xù)傳播。那么如何讓兩個有層次關系并且都添加了手勢的控件都能正確識別手勢呢?答案就是利用代理的-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer方法。這個代理方法默認返回NO,會阻斷繼續(xù)向下識別手勢,如果返回YES則可以繼續(xù)向下傳播識別。
這篇文章參考至:【Kenshin Cui's Blog】的【iOS開發(fā)系列--觸摸事件、手勢識別、搖晃事件、耳機線控】