Posts

Post not yet marked as solved
0 Replies
4.2k Views
Here is my view hierarchy: https://i.stack.imgur.com/gI1xc.png class ViewControllerTabOne: TabViewController, WKNavigationDelegate { @IBOutlet weak var glassView: UIView! @IBOutlet weak var responseWebView: WKWebView! var portionRect: CGRect! override func viewDidLoad() { super.viewDidLoad() self.view.isUserInteractionEnabled = true self.responseWebView!.navigationDelegate = self loadWebsite() // Do any additional setup after loading the view. } func loadWebsite() { self.urlReq = URLRequest(url: URL(string: "https://www.google.com/")!) self.responseWebView.load(self.urlReq!) } func simulateTap() { let pointOnTheScreen = CGPoint(x: 156.0, y: 506.6) self.glassView.hitTest(pointOnTheScreen, with: nil) self.glassView.overlapHitTest(point: pointOnTheScreen, withEvent: nil) print("tap on screen") } override func touchesBegan(_ touches: Set, with event: UIEvent?) { if let touch = touches.first { let touchLocation = touch.location(in: self.view) print("point touched: \(touchLocation)") } super.touchesBegan(touches, with:event) } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { print("Finished navigating to url \(String(describing: webView.url))") self.simulateTap() } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } extension UIView { func overlapHitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { // 1 if !self.isUserInteractionEnabled || self.isHidden || self.alpha == 0 { return nil } //2 var hitView: UIView? = self if !self.point(inside: point, with: event) { if self.clipsToBounds { return nil } else { hitView = nil } } //3 for subview in self.subviews.reversed() { let insideSubview = self.convert(point, to: subview) if let sview = subview.overlapHitTest(point: insideSubview, withEvent: event) { return sview } } return hitView } } class PassThroughView: UIView { override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { for subview in subviews { if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) { return true } } return false } override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let view = super.hitTest(point, with: event) if view == self { return nil //avoid delivering touch events to the container view (self) } else { return view //the subviews will still receive touch events } } }Essentially, I have the GlassView OVER the Web View and I want to be able to interact with the Web View as if there is nothing over it while being able to simulate a tap on a webview button while being able to see its x and y coordinates of a tap (so i can automate a process on a webview)Mind you that it is all within a TABBARCONTROLLERI need to be able to send through touch events to record the locations and then simulate touch events on the wkwebviewCurrent code is able to interact with wkwebview as if glassview is not over it, but the simulatetap function does nothing and i do not see touchesbegan fire off with locations.I am referencing;https://stackoverflow.com/questions/3046813/how-can-i-click-a-button-behind-a-transparent-uiview/4010809#4010809https://stackoverflow.com/questions/38075699/swift-programmatically-click-on-point-in-screen
Posted Last updated
.