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

How to implement sound and vibration together in SwiftUI

注释onTapGesture 激活声音。但是振动不起作用。如何同时实现声音和振动?

为什么会出现这个问题?

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()
}