Use SwiftUI-like syntax to create haptic feedback patterns.
struct CircleHaptics: HapticSequence {
var haptics: HapticEvents {
for i in 0..<11 {
let intensity = (Float(i) / 11) * 0.8 + 0.1
Continuous(duration: 0.1,
intensity: intensity,
sharpness: Float(i) / 11)
}
for i in 0..<4 {
let intensity = 1 - (Float(i) / 4) * 0.8 + 0.1
Continuous(duration: 0.1,
intensity: intensity,
sharpness: 1 - (Float(i) / 4))
}
Transient(intensity: 1, sharpness: 1)
Wait(duration: 0.2)
Transient(intensity: 1, sharpness: 0.5)
}
}
- Add the Swift Package: https://github.com/jiachenyee/buzz
- Create a new file to store your haptics.
- Type/Paste in the boilerplate
struct MyHaptics: HapticSequence {
var haptics: HapticEvents {
Continuous(duration: 1,
intensity: 0.5,
sharpness: 0.5)
}
}
- Play it by using
MyHaptics.play()
.
Button("Play") {
MyHaptics.play()
}
I've built a demo project that has a circle animation demo along with a heartbeat one. Download it and play around here.
Lazy to type in all that boilerplate? Use the .codesnippet
.
- Quit Xcode and open Finder
- Press Command-Shift-G and paste in "~/Library/Developer/Xcode/UserData/CodeSnippets"
- Drag the Buzz.codesnippet file into it.
- Relaunch Xcode
- Now you can access it by typing
Buzz
in Xcode, or by pressing Command-Shift-L to open your Library.
MIT.