SwiftUI 菜单 - 菜单实际打开时的操作

标签 swiftui menu haptic-feedback

我的应用程序中有一个菜单,当菜单打开时我会触发触觉反馈(来自 onTagGesture Action )。
然而 有时当我点击菜单打开触发器时,菜单实际上不会打开,但我仍然收到触觉反馈。我只在菜单实际打开时才想要触觉。
这是代码简化的代码块:

Menu {
    Button("Menu button", action: someAction}}
} label: { 
    Text("Open menu") //in reality, this a more complicated view tapping on which should open the (context) menu
}
.onTagGesture {
  let generator = UIImpactFeedbackGenerator(style: .rigid)
  generator.impactOccurred()
}
所以这很简单 - 点击 Open menu触发,点击被注册,触觉被播放,菜单打开。
但如前所述,无论出于何种原因,有时我都会按下 Open menu元素,触觉播放,但实际菜单不会打开。
无论是什么原因,我想知道一旦菜单实际打开(或者更好的是, 实际打开),是否有任何方法可以执行操作(例如前面提到的触觉反馈)?我试着到处找,但一无所获。
这也很重要因为菜单也会在长按时打开 ,这是用于打开菜单的 iOS 标准操作。即使我可以为长按添加另一个单独的处理程序(为两种情况提供触觉),但这似乎根本不是一种正确的方法。
结合有时菜单无法打开的事实,我似乎确实需要一些其他解决方案。任何人都可以分享任何想法吗?是否有某种我缺少的 onXXXXX 处理程序,它会在菜单打开时触发吗?
谢谢!
PS:为了提供更多细节,我正在尝试对 Apple 开发文档中描述的菜单实现这种方法:
https://developer.apple.com/documentation/swiftui/menu
作为过程的一部分,我尝试将 onAppear 处理程序附加到整个菜单以及菜单中的单个元素。两者似乎都不起作用。
Menu {
    Button("Open in Preview", action: openInPreview)
    Button("Save as PDF", action: saveAsPDF)
        .onAppear { doHaptic() } //only fires once, when menu opens, but not for subsequent appearances
} label: {
    Label("PDF", systemImage: "doc.fill")
}
.onAppear { doHaptic() } //doesn't really as it fires when the menu itself appears on the screen as a child of a parent view.

最佳答案

您可以使用 onAppear为了那个原因。在菜单上使用它,只有在菜单出现时才会调用它。例如。以下:

struct ContentView: View {
    
    @State var menuOpen: Bool = false
    
    // Just your button that triggers the menu
    var body: some View {
        Button(action: {
            self.menuOpen.toggle()
        }) {
            if menuOpen {
                MenuView(menuOpen: $menuOpen)
            } else {
                Image(systemName: "folder")
            }
        }
    }
}


struct MenuView: View {
    
    @Binding var menuOpen: Bool
    // the menu view
    var body: some View {
        Rectangle()
            .frame(width: 200, height: 200)
            .foregroundColor(Color.red)
            .overlay(Text("Menu Open").foregroundColor(Color.white))
            .onAppear(perform: self.impactFeedback) // <- Use onAppear on the menu view to trigger it
    }
    
    // if function called it triggers the impact
    private func impactFeedback() {
        let generator = UIImpactFeedbackGenerator(style: .rigid)
        generator.impactOccurred()
        print("triggered impact")
    }
}

在 Xcode 12.4 上测试和工作

关于SwiftUI 菜单 - 菜单实际打开时的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66717326/

相关文章:

swiftui - 在列表 (SwiftUI) 中导航回后,所选列表行的背景保持灰色(选中)。 iOS14 + Xcode12

function - SwiftUI - 将图像从函数返回到 View

html - 带有事件链接的简单水平菜单

html - 菜单问题和显示尺寸

android - Xamarin Android 菜单图标不显示

Flutter:如何使用 HapticFeedback

swiftui - 叠加在内联导航栏顶部

swift - 为什么在初始化程序中设置时调用 didSet?

ios - 是否可以在 Apple Watch OS 2 上触发没有伴随声音的触觉反馈

swift - 我收到错误 : Use of unresolved identifier 'UINotificationFeedbackGenerator'