UITableView 嵌套 WKWebView

需求是 UITableView 里面嵌套一个 WKWebView 用于展示 商品详情的图片。图片需要有双指放大效果。于是就涉及到了两个 UIScrollView 的滑动手势冲突问题。

1、刚开始想出的方案是通过设置 scrollEnabled 或者 bounces = NO 来调整 哪个 scrollView 响应事件,在高系统版本上还好,但是在 iOS8上就不行了,一旦 webView 接收到 了滑动事件,不管怎么调整,都不会再响应到 tableView 上了。

2、然后又想通过 设置 UIScrollView 的滑动手势的 delegate,来控制哪个先响应。但是 UIScrollView 的 panGesture 的 delegate 必须设置为 scrollView 自己,于是继承 UITableView,来重载 panGesture 的 delegate 方法。但是继承后 发现这些 方法 虽然 可以调用到,但是调用 super 是个问题。而且继承 WKWebView 里面的 scrollView 也不敢去尝试,害怕WK对它有做不知道的设置。于是此方案也是行不通。

3、最后在翻看 SDK 时,发现 UIView 居然有一个 gesturedelegate 方法:

// called when the recognizer attempts to transition out of UIGestureRecognizerStatePossible if a touch hit-tested to this view will be cancelled as a result of gesture recognition
// returns YES by default. return NO to cause the gesture recognizer to transition to UIGestureRecognizerStateFailed
// subclasses may override to prevent recognition of particular gestures. for example, UISlider prevents swipes parallel to the slider that start in the thumb
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer NS_AVAILABLE_IOS(6_0);

于是就用 swizzle 直接把这个方法弄出来,然后再这里针对两个 scrollView 做出对应的判断就好了。发现效果还可以,能够接收。

哎,原来方法是如此简单。把 UIView 的 常用方法都给忘了。这里记录下,希望以后不要再忘了。

你可能感兴趣的:(UITableView 嵌套 WKWebView)