如何在SwiftUI中同时实现声音和振动



对TapGesture进行注释会激活声音。但是振动不起作用。如何将声音和振动结合起来?

为什么会出现这个问题?

let feedback = UIImpactFeedbackGenerator(style: .soft)
func activeNum() {
playSound(sound: "casino-chips", type: "mp3")
}
var body: some View {
ZStack {
VStack(spacing: 0) {
LogoView()

Spacer()

Button(action: {
self.activeNum()
}) {
Image(systemName: "heart.fill")
.resizable()
.frame(width: 100, height: 100, alignment: .center)
.foregroundColor(.gray)
//                        .onTapGesture {
//                            feedback.impactOccurred()
//                        }
}

Spacer()
}
}
}

Button操作中,同时调用activeNum()impactOccured(),而不是尝试将后者放在单独的onTapGesture:中

Button(action: {
self.activeNum()
feedback.impactOccurred()
}

最新更新