【发布时间】:2015-12-21 00:13:39
【问题描述】:
我正在为我的应用程序使用 Swift 和 XCTest 编写 UI 测试,我必须摇动设备才能获得通知,你能告诉我如何在代码中生成摇动手势吗?
【问题讨论】:
-
您找到解决方案了吗?
我正在为我的应用程序使用 Swift 和 XCTest 编写 UI 测试,我必须摇动设备才能获得通知,你能告诉我如何在代码中生成摇动手势吗?
【问题讨论】:
使用 XCUITest 时仍然不支持摇动手势。如果可以的话,我建议你做一个解决方法(两指轻敲?),或者使用支持摇动的 Appium 之类的工具。
【讨论】:
override var canBecomeFirstResponder: Bool {
return true
}
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Shaked")
}
}
【讨论】:
首先您应该将以下方法添加到您的UIViewController 子类中:
override func canBecomeFirstResponder() -> Bool {
return true
}
接下来是添加motionEnded:withEvent:方法:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
if motion == .MotionShake {
print("Shaked")
}
}
祝你好运:)
【讨论】: