本帖最后由 小文0111 于 2019-3-13 16:24 编辑
首先webView 上添加两个手势, - <p style="line-height: 28px;"> // 点击webView 中心区域,从上面和下面弹出两个菜单栏</p><p style="line-height: 28px;"> UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWebView:)];</p><p style="line-height: 28px;"> tap.delegate = self;</p><p style="line-height: 28px;"> [self.webView addGestureRecognizer:tap];</p><p style="line-height: 28px;"> // 长按webView弹出 UIMenuViewController 增加复制 地图 百科等功能.</p><p style="line-height: 28px;"> UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressWebView:)];</p><p style="line-height: 28px;"> [longPressGesture setDelegate:self];</p><p style="line-height: 28px;"> [self.webView addGestureRecognizer:longPressGesture];</p>
复制代码
要想让点击手势起作用需要实现 : - <p style="line-height: 28px;">- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {</p><p style="line-height: 28px;"> if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {</p><p style="line-height: 28px;"> return YES;</p><p style="line-height: 28px;">}</p>
复制代码
这样就技能响应点击手势 又能响应长按手势了,但是还有一个问题,那就是长按 webView 中心区域时 既会出现UIMenuController 又会弹出上下菜单栏......! 为了解决这个问题就需要继续在这个手势的代理方法中做手脚了. - <p style="line-height: 28px;">- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {</p><p style="line-height: 28px;"> /**</p><p style="line-height: 28px;"> * 如果第一个手势是点击第二个是长按就返回NO 不支持同时响应长按和点击手势</p><p style="line-height: 28px;"> */</p><p style="line-height: 28px;"> if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {</p><p style="line-height: 28px;"> return NO;</p><p style="line-height: 28px;"> } else {</p><p style="line-height: 28px;"> return YES;</p><p style="line-height: 28px;"> }</p><p style="line-height: 28px;">}</p>
复制代码
这样一来 长按中心区域就只会出现,U了 而不会弹出上下菜单栏. tap 和 longPress手势又都能响应了! -------- ::要想让UIMenuController弹出菜单控制器必须实现: - <p style="line-height: 28px;">-(BOOL)canBecomeFirstResponder</p><p style="line-height: 28px;">{</p><p style="line-height: 28px;"> return YES;</p><p style="line-height: 28px;">}</p><p style="line-height: 28px;">---复制的实现</p>
- <p style="line-height: 28px;">-(void)menuCopy:(id)sender</p><p style="line-height: 28px;">{</p><p style="line-height: 28px;"> [self.webView performSelector:@selector(copy:)];</p><p style="line-height: 28px;">} </p>
复制代码
选择和全选的实现同copy!
|