Version
14.0
5.7
12.6
16.0
この記事では、Swiftのバイブレーション(振動)の実装方法を解説していきたいと思います。
バイブレーションの実装方法
バイブレーションには、たくさんの種類があります。強いバイブ、弱いバイブ、数回のバイブなどあります。
また、エラーの時や、成功の時でもバイブの振動の感じが違うので、時と場合にあったバイブを使うことを心がけましょう。
実装方法は、鳴らしたいところに記述するだけです。
以下のコードを貼り付けて実機で試してみてください。
import SwiftUI
import AudioToolbox
struct ContentView: View {
@State private var isVibrationOn = false
var body: some View {
VStack(spacing: 20.0) {
// デフォルト使えるバイブ
Group {
Button("成功時のバイブ") {
UINotificationFeedbackGenerator().notificationOccurred(.success)
}
Button("失敗時のバイブ") {
UINotificationFeedbackGenerator().notificationOccurred(.error)
}
Button("警告時のバイブ") {
UINotificationFeedbackGenerator().notificationOccurred(.warning)
}
Button("軽いバイブ") {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}
Button("普通のバイブ") {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
}
Button("強いバイブ") {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
}
Button("フィードバック時のバイブ") {
UISelectionFeedbackGenerator().selectionChanged()
}
}
// AudioToolboxを使ったバイブ
Group {
Button("強い長めのバイブ(kSystemSoundID_Vibrate)") {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {}
}
Button("1102") {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1102)) {}
}
Button("1519") {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1519)) {}
}
Button("1520") {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1520)) {}
}
Button("1521") {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1521)) {}
}
}
Group {
Button("数回バイブ") {
for _ in 0...2 {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {}
sleep(1)
}
}
Toggle("繰り返しバイブ", isOn: $isVibrationOn)
.onChange(of: isVibrationOn) { _ in
makeVibrationNoLimit()
}
}
}
.padding(.all, 30.0)
}
func makeVibrationNoLimit() {
if !isVibrationOn { return }
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {
makeVibrationNoLimit()
sleep(1)
}
}
}
デフォルト使えるバイブ
成功時のバイブ
UINotificationFeedbackGenerator().notificationOccurred(.success)
失敗時のバイブ
UINotificationFeedbackGenerator().notificationOccurred(.error)
警告時のバイブ
UINotificationFeedbackGenerator().notificationOccurred(.warning)
軽いバイブ
UIImpactFeedbackGenerator(style: .light).impactOccurred()
普通のバイブ
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
強いバイブ
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
フィードバック時のバイブ
UISelectionFeedbackGenerator().selectionChanged()
AudioToolboxを使ったバイブ
以下のバイブを使うには、import AudioToolboxが必要です。
強い長めのバイブ(kSystemSoundID_Vibrate)
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {}
1102
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1102)) {}
1519
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1519)) {}
1520
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1520)) {}
1521
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(1521)) {}
他にも色々とあります。ただ、この一覧表みたいな記事は見つけられませんでした。こちらの記事に一覧表の記載ありますが、全然足りないようです。。
数回鳴らす方法
for _ in 0...2 {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {}
sleep(1)
}
繰り返し鳴らす方法
以下の関数を呼ぶ
func makeVibrationNoLimit() {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {
makeVibrationNoLimit()
sleep(1)
}
}

