Swift-以编程方式单击屏幕上的点
tl; dr-如何在屏幕上的特定位置上以编程方式执行本机触摸?
tl;dr- How can I programmatically perform a native touch in specific point on the screen?
在网络视图中,包含HTML的HTML包含Iframe,因此无法访问网络视图代码和元素,也无法对JS进行打磨. Web视图中的特定坐标上有一个按钮.如何以编程方式按下它?
Within the web view there is HTML that contains an Iframe, so the web view code and elements are not accessible and sanding massages to JS is not possible either. There is a button in the web view on specific coordinates. How can I press it programmatically?
If you don't want to simulate the touch on the JavaScript level, you can use this extension on UITouch
and UIEvent
, just bridge it to Swift:
func performTouchInView(view: UIView) {
let touch = UITouch(inView: view)
let eventDown = UIEvent(touch: touch)
touch.view!.touchesBegan(eventDown.allTouches()!, withEvent: eventDown)
touch.setPhase(.Ended)
let eventUp = UIEvent(touch: touch)
touch.view!.touchesEnded(eventUp.allTouches()!, withEvent: eventUp)
}
根据使用的Swift版本,您的语法可能会有所不同.我在这里进行一些强制展开操作,但是您应该大致了解发生了什么.
Your syntax may vary depending on the version of Swift you use. I'm doing some forced-unwrapping here but you should get the general idea of what is happening.