ios - 以编程方式在屏幕上执行点击
问题描述
有没有办法以编程方式在屏幕上执行点击?
我需要通过用户点击访问无法以编程方式访问的框架按钮。
因此,我需要在用户点击屏幕时模拟点击,该位置的组件会响应,例如按钮。
解决方案
这是我在测试中使用的东西:
class TouchMock: UITouch {
var y: CGFloat
init(y: CGFloat) {
self.y = y
super.init()
}
override func location(in view: UIView?) -> CGPoint {
return CGPoint(x: 10, y: y)
}
}
func sendTouchesBegan(y: CGFloat, to view: UIView) {
let touch = TouchMock(y: y)
view.touchesBegan([touch], with: nil)
}
func sendTouchesMoved(y: CGFloat, to view: UIView) {
let touch = TouchMock(y: y)
view.touchesMoved([touch], with: nil)
}
func sendTouchesEnded(y: CGFloat, to view: UIView) {
let touch = TouchMock(y: y)
view.touchesEnded([touch], with: nil)
}
func sendTouchesCancelled(y: CGFloat, to view: UIView) {
let touch = TouchMock(y: y)
view.touchesCancelled([touch], with: nil)
}
重要提示:对我来说,只有 Y 坐标很有趣。添加 X 坐标当然是微不足道的。
推荐阅读
- javascript - 如何存储json下拉列表中的项目
- objective-c - 屏幕锁定时如何将数据发送到隐藏设备
- javascript - 使用参数时Nodejs中的类不返回预期值
- flutter - 如何在飞镖中制作超级构造函数
- c++ - 一起使用 new 和 & 符号
- java - PDF 垂直剪切并合并
- swiftui - SwiftUI - 开始/结束外观转换的不平衡调用
- reactjs - React Native Deck Swiper
- go - 在 Windows 中用另一个版本替换 go-swagger
- cryptography - 格式化 Chainlink AggregatorV3Interface 对美元的“答案”?