【问题标题】:How to generate Shake Gesture using Swift (XCTest)如何使用 Swift (XCTest) 生成摇动手势
【发布时间】:2015-12-21 00:13:39
【问题描述】:

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

【问题讨论】:

  • 您找到解决方案了吗?

标签: ios xcode swift xctest


【解决方案1】:

使用 XCUITest 时仍然不支持摇动手势。如果可以的话,我建议你做一个解决方法(两指轻敲?),或者使用支持摇动的 Appium 之类的工具。

【讨论】:

    【解决方案2】:

    斯威夫特 3

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

    【讨论】:

    • 这是如何处理摇动手势,而不是执行一个
    【解决方案3】:

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

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

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

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

    祝你好运:)

    【讨论】:

    • 我不必在 Swift 2 中使用 canBecomeFirstResponder() 委托。*
    • 这没有回答如何从代码/测试中生成摇动手势。
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多