首先webView 上添加兩個手勢,web
// 點擊webView 中心區域,從上面和下面彈出兩個菜單欄代理
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWebView:)];orm
tap.delegate = self;get
[self.webView addGestureRecognizer:tap];it
// 長按webView彈出 UIMenuViewController 增長複製 地圖 百科等功能.io
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressWebView:)];form
[longPressGesture setDelegate:self];class
[self.webView addGestureRecognizer:longPressGesture];select
要想讓點擊手勢起做用須要實現 :方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
這樣就技能響應點擊手勢 又能響應長按手勢了,可是還有一個問題,那就是長按 webView 中心區域時 既會出現UIMenuController 又會彈出上下菜單欄......!
爲了解決這個問題就須要繼續在這個手勢的代理方法中作手腳了.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
/**
* 若是第一個手勢是點擊第二個是長按就返回NO 不支持同時響應長按和點擊手勢
*/
if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return NO;
} else {
return YES;
}
}
這樣一來 長按中心區域就只會出現,U了 而不會彈出上下菜單欄. tap 和 longPress手勢又都能響應了!
--------
::要想讓UIMenuController彈出菜單控制器必須實現:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
---複製的實現
-(void)menuCopy:(id)sender
{
[self.webView performSelector:@selector(copy:)];
} 選擇和全選的實現同copy!