【问题标题】:Shake gesture not working iOS摇动手势不起作用iOS
【发布时间】:2018-01-19 13:29:50
【问题描述】:

我使用了collectionView,并且还在我的 ViewController(命名为:“DashboardViewController”)中实现了拉取同步(pull_Down_To_Get_Data_From_Server)。我试过摇晃手势 我的DashboardViewController 中的代码(如下所示)并且它不工作,但类似的代码在同一应用程序的另一个viewController 中工作。

我第一次尝试在viewDidLoad 中使用canBecomeFirstResponder,第二次在viewDidAppear 中使用,但它也没有用。

要求: 实际上我想知道“.motionShake”事件不直接触发并且我们必须以另一种方式实现的情况(如果有的话)?而且我没有发现任何富有成果的东西 在谷歌上。 Image of storyboard may help in understanding all the utilities used

override func viewDidLoad() {
 // call super
    super.viewDidLoad()
    self.becomeFirstResponder() // for shake gesture detection
}

// it will make first responder to detect shake motion event
override var canBecomeFirstResponder: Bool {
    get {
        return true
    }
}

// get detection of shake motion event
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake 
    {
        print("Shake detected")
    }
}

【问题讨论】:

  • 您的代码似乎是从stackoverflow.com/a/43151351/1226963复制的
  • 实际上这段代码是用来获取摇动手势动作的,我用它是为了我自己的缘故,你使用这段代码给出的参考是为了他的目的。代码可能相似,因为它是供使用的,但不同之处在于我们使用它的方法@rmaddy

标签: ios iphone swift uigesturerecognizer handshake


【解决方案1】:

我们在处理 Yoshi 时遇到了类似的情况

我们最终在 UIWindow 上创建了一个扩展,其中包含 motionBegan 事件并转发了该事件。不知道是什么原因造成的,但它似乎与 swift 3.0 中断了。

【讨论】:

  • 我喜欢这个答案。我不知道为什么,但在视图控制器级别(较旧的代码库;Xcode 8 中的 Swift 2.3)实现 motionBegan 函数没有任何成功。我在 UIWindow 的扩展中实现了它,我发布了一个通知,它运行良好。
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多