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

问题描述

我的应用中有一个菜单,当菜单打开时我会触发触觉反馈(来自 onTagGesture 操作)。

但是有时当我点击菜单打开触发器时,菜单实际上不会打开,但我仍然收到触觉反馈。我只在菜单实际打开时才需要触觉。

这里是代码简化的代码块:

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 上测试和工作