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")
}
}
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
isShakingproperty to an array of structs that holds the settings for your table view cells.) In yourcellForRowAt()method, check the model to see if yourisShakingproperty for thatIndexPathis true, and if it is, make whatever change to your cell animation that you need.