如何在iOS中停止摇动手势?

时间:2017-09-11 04:39:37

标签: ios swift uigesturerecognizer

我使用摇动手势在我的应用中创建新帖子。如何在入职者屏幕上停止摇动手势?

override func motionEnded(_ motion: UIEventSubtype,
                              with event: UIEvent?) {
    if motion == .motionShake {
        if self.revealtype == "opened" {
            self.revealViewController().revealToggle(self)
        } 

        datearray.removeAll()

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy"
        let monthdate = dateFormatter.string(from: NSDate() as Date)

        datearray.append(monthdate)
        selectnsdate  = Date()
        print("success")

        let ivc = self.storyboard!.instantiateViewController(withIdentifier: "selectpost") as? SelectPost
        ivc?.shake = true
        self.navigationController!.pushViewController(ivc!, animated: true)
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在viewWillAppear上添加摇动手势,并在viewDidDisappear viewController的方法中删除手势。因此,当您按下另一个视图控制器时,摇动手势将不会触发。

答案 1 :(得分:0)

您应该可以通过在您的入职视图控制器中实现以下功能来吞下motionShake事件来实现此目的:

override func motionEnded(_ motion: UIEventSubtype,
                         withEvent: UIEvent?) {
    if motion != .motionShake {
        super.motionEnded(motion, withEvent: withEvent)
    }
}

motionEnded的文档声明"此方法的默认实现将消息转发到响应者链上#34;。