如何使用Swift(XCTest)生成Shake Gesture

时间:2015-09-23 15:25:44

标签: ios xcode swift xctest

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

3 个答案:

答案 0 :(得分:2)

Swift 3

override var canBecomeFirstResponder: Bool {
    return true
}

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Shaked")
    }
}

答案 1 :(得分:-1)

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

override func canBecomeFirstResponder() -> Bool {
    return true
}

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

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {
      print("Shaked")
    }
}
祝你好运:)

答案 2 :(得分:-1)

使用XCUITest时仍然不支持摇动手势。如果可以,或者建议使用支持摇晃功能的Appium之类的工具,我建议您进行变通(两指点击?)。