如何使用 Swift 生成摇动手势 (XCTest)

Vik*_*ngh 5 xcode ios xctest swift

我正在使用 Swift 和 XCTest 为我的应用程序编写 UI 测试,我必须摇动设备才能获取通知,您能告诉我如何在代码中生成摇动手势吗?

Aao*_*oIi -2

首先,您应该将以下方法添加到您的UIViewController子类中:

override func canBecomeFirstResponder() -> Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

接下来是添加motionEnded:withEvent:方法:

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {
      print("Shaked")
    }
}
Run Code Online (Sandbox Code Playgroud)

祝你好运 :)