How to detect shake in UITableViewCell

107 views Asked by At

I want to detect a shake gesture in a UITableViewCell, but no shake is being registered. Is there a way I can detect a shake in a UITableView cell?

This is the code I'm using now:

extension MyTableViewCell {
    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        print("shake started")
    }
    
    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        print("shake stopped")
    }
}
1

There are 1 answers

0
Duncan C On

I would suggest adding your motionBegan/motionEnded methods to your view controller, and then updating the model that backs your table view to reflect the shake event and tell the table view to reload. (For example, you might add an isShaking property to an array of structs that holds the settings for your table view cells.) In your cellForRowAt() method, check the model to see if your isShaking property for that IndexPath is true, and if it is, make whatever change to your cell animation that you need.