You want to implement shaking gestures this means you actually want to use a shake gesture for undo or redo.
In this case we are simply going to update a
UILabel to show that the device has been shaken n times (beginning at zero).
We have a single
ViewController. We create this basic view controller with a
shakeCounter property that is displayed on the
We need to force this to receive touch events. That is, we make
ViewController the first
ViewController in the responder chain using the instance method
Handle the event using
motionEnded(_:with:) within the
ViewController. In this case, we are simply going to update the counter and the
ViewController looks like the following:
There is a repo with the full project, if you want to download it.
I hope this article has helped you to implement shake gestures in Swift.