在平时的开发中,要使用到webview,但类似微信的webview在数据没有加载完成的时候
双击屏幕,webview不会缩放,其实实现这个功能很简单
代码是用swift写的
class YYSimpleWebViewController: UIViewController,UIWebViewDelegate,UIGestureRecognizerDelegate { var urlS :NSURL?
var cusWebView :UIWebView!
var isLoaded = false
let cover = UIView() init(WithUrl webUrl :NSURL?) {
super.init(nibName: nil, bundle: nil)
self.urlS = webUrl
cusWebView = UIWebView()
cusWebView.delegate = self
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity =
NSURLCache.sharedURLCache().memoryCapacity = } required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} override func viewDidLoad() {
super.viewDidLoad()
cusWebView.frame = self.view.bounds
self.view.addSubview(cusWebView) cover.backgroundColor = UIColor.clearColor()
cover.frame = cusWebView.bounds;
cusWebView.scrollView.addSubview(cover) print(cusWebView.gestureRecognizers)
let doubleTap = UITapGestureRecognizer(target: self, action: "tap")
doubleTap.numberOfTapsRequired =
doubleTap.delegate = self
cover.addGestureRecognizer(doubleTap) if self.urlS != nil { cusWebView.loadRequest(NSURLRequest(URL: self.urlS!))
} }
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return true
} func webViewDidFinishLoad(webView: UIWebView) { self.cover.removeFromSuperview()
self.title = webView.stringByEvaluatingJavaScriptFromString("document.title")
} func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
self.cover.removeFromSuperview()
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { return true
} func tap() { }
核心思想就是在在webview的上边加一个透明的view,加载完成后移除,在加载过程中,拦截双击事件