A SwiftUI View that easily brings tap tempo functionality to music apps.
The TapTempoButton
takes a content closure so it can be customized. The whole view will handle the tap events automatically and calculate the tempo average (in BPM) which is sent in the onTempoChange
closure on each change.
Rather than processing the touch events like a standard Button (on touch up) the view handles them on touch down so the button behaviour is more natural in a musical context.
Add the TapTempoButton
to your View and customize it with the content closure.
TapTempoButton(onTempoChange: {
self.tempo = $0
}) {
Text("Tap")
}
It can also be customized like a regular SwiftUI Button:
.buttonBorderShape(.capsule)
.buttonStyle(.bordered)
or
.buttonStyle(MyButtonStyle())
The following properties can be configured on initialization:
tempoRange
: Defines the minimum and maximum tempo that can be detected.timeout
: Seconds of inactivity after which the ongoing detection will be restarted.minTaps
: Minimum number of taps required before sending values to theonTempoChange
closure.roundDecimals
: Number of decimals to round the BPM to. Set to0
for integer-only BPM (no decimals). Set tonil
to disable rounding.
- From the File menu, select Add Packages...
- Enter package repository URL: https://github.com/yannxou/TapTempoButton
- Confirm the version and let Xcode resolve the package
The idea for this library appeared while working on the app Vetro: Visual Metronome for iOS/macOS. If you like it you can support us by buying any of our apps.
This library is released under the MIT license. See LICENSE for details.