简体   繁体   English

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

[英]How to stop shake gesture in iOS?

I'm using shake gesture for creating new post in my app. 我正在使用摇动手势在应用中创建新帖子。 How to stop shake gesture during Onboarding screen? 如何在入职屏幕期间停止摇动手势?

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)
    }
}

You can add shake gesture on viewWillAppear and remove gesture on viewDidDisappear viewController's methods. 您可以在viewWillAppear上添加摇动手势,并在viewDidDisappear viewController的方法上删除手势。 So when you push another view controller shake gesture will not trigger. 因此,当您按下另一个视图控制器时,不会触发摇动手势。

You should be able to achieve this by simply swallowing motionShake events by implementing the following in your onboarding view controller: 通过在入职视图控制器中实现以下内容,只需吞入motionShake事件即可实现此目的:

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

(The documentation for motionEnded states that "the default implementation of this method forwards the message up the responder chain".) motionEnded的文档指出“此方法的默认实现将消息转发到响应者链上。”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM