はじめに
iOS17でsensoryFeedback
というメソッドが追加されました。
これを使うことでSwiftUIで触覚フィードバックを実装することができます。
実装
import SwiftUI
struct ContentView: View {
@State private var start = false
@State private var stop = false
@State private var alignment = false
@State private var decrease = false
@State private var increase = false
@State private var levelChange = false
@State private var selection = false
@State private var success = false
@State private var warning = false
@State private var error = false
@State private var impact = false
@State private var impactWeightLight = false
@State private var impactWeightMedium = false
@State private var impactWeightHeavy = false
@State private var impactFlexibilityRigid = false
@State private var impactFlexibilitySoft = false
@State private var impactFlexibilitySolid = false
var body: some View {
List {
Section {
Button {
start.toggle()
} label: {
Text("start")
}
Button {
stop.toggle()
} label: {
Text("stop")
}
} header: {
Text("Indicating start and stop")
}
Section {
Button {
alignment.toggle()
} label: {
Text("alignment")
}
Button {
decrease.toggle()
} label: {
Text("decrease")
}
Button {
increase.toggle()
} label: {
Text("increase")
}
Button {
levelChange.toggle()
} label: {
Text("levelChange")
}
Button {
selection.toggle()
} label: {
Text("selection")
}
} header: {
Text("Indicating changes and selections")
}
Section {
Button {
success.toggle()
} label: {
Text("success")
}
Button {
warning.toggle()
} label: {
Text("warning")
}
Button {
error.toggle()
} label: {
Text("error")
}
} header: {
Text("Indicating the outcome of an operation")
}
Section {
Button {
impact.toggle()
} label: {
Text("impact")
}
Button {
impactWeightLight.toggle()
} label: {
Text("impact(light)")
}
Button {
impactWeightMedium.toggle()
} label: {
Text("impact(medium)")
}
Button {
impactWeightHeavy.toggle()
} label: {
Text("impact(heavy)")
}
Button {
impactFlexibilityRigid.toggle()
} label: {
Text("impact(rigid)")
}
Button {
impactFlexibilitySoft.toggle()
} label: {
Text("impact(soft)")
}
Button {
impactFlexibilitySolid.toggle()
} label: {
Text("impact(solid)")
}
} header: {
Text("Producing a physical impactin")
}
}
.sensoryFeedback(.start, trigger: start)
.sensoryFeedback(.stop, trigger: stop)
.sensoryFeedback(.alignment, trigger: alignment)
.sensoryFeedback(.decrease, trigger: decrease)
.sensoryFeedback(.increase, trigger: increase)
.sensoryFeedback(.levelChange, trigger: levelChange)
.sensoryFeedback(.selection, trigger: selection)
.sensoryFeedback(.success, trigger: success)
.sensoryFeedback(.warning, trigger: warning)
.sensoryFeedback(.error, trigger: error)
.sensoryFeedback(.impact, trigger: impact)
.sensoryFeedback(.impact(weight: .light), trigger: impactWeightLight)
.sensoryFeedback(.impact(weight: .medium), trigger: impactWeightMedium)
.sensoryFeedback(.impact(weight: .heavy), trigger: impactWeightHeavy)
.sensoryFeedback(.impact(flexibility: .rigid), trigger: impactFlexibilityRigid)
.sensoryFeedback(.impact(flexibility: .soft), trigger: impactFlexibilitySoft)
.sensoryFeedback(.impact(flexibility: .solid), trigger: impactFlexibilitySolid)
}
}
解説
.sensoryFeedback(触覚フィードバックの種類, trigger: 触覚フィードバックを実行するトリガー)
おわり
UIKitの時にはなかったものが追加されていますね