小编Ibr*_*m99的帖子

是否可以在 SwiftUI 中突出显示鼠标悬停时的行

我正在尝试创建一个列表,其中的行在鼠标经过时会更改背景颜色。但使用当前代码,所有行都会在悬停时突出显示。

struct ContentView: View {
    @State private var overText = false
    
    var body: some View {
        
        List{
            ForEach(0 ..< 3){ item in
                Row(text: "hhaha")
                        .background(Color(overText ? .systemBlue : .clear))
                        .onHover(perform: { hovering in
                            overText.toggle()
                        })
            }
        }
     
    }
}
Run Code Online (Sandbox Code Playgroud)

这是当前结果

在此输入图像描述

这就是我想要实现的目标

在此输入图像描述

swift swiftui swiftui-list

2
推荐指数
1
解决办法
2087
查看次数

在快速点击按钮时显示上下文菜单

在 iOS 中,可以通过长按或点击来显示上下文菜单。目前,以下代码在长按时显示上下文菜单,如何在轻按时显示菜单?

    let interaction = UIContextMenuInteraction(delegate: self)
    tagBtn.addInteraction(interaction)

    func contextMenuInteraction(_ interaction: UIContextMenuInteraction,
      configurationForMenuAtLocation location: CGPoint)
      -> UIContextMenuConfiguration? {

      let favorite = UIAction(title: "Favorite",
        image: UIImage(systemName: "heart.fill")) { _ in
        // Perform action
      }

      let share = UIAction(title: "Share",
        image: UIImage(systemName: "square.and.arrow.up.fill")) { action in
        // Perform action
      }

      let delete = UIAction(title: "Delete",
        image: UIImage(systemName: "trash.fill"),
        attributes: [.destructive]) { action in
         // Perform action
       }

       return UIContextMenuConfiguration(identifier: nil,
         previewProvider: nil) { _ in
         UIMenu(title: "Actions", children: [favorite, share, delete]) …
Run Code Online (Sandbox Code Playgroud)

contextmenu menu swift

2
推荐指数
1
解决办法
1545
查看次数

标签 统计

swift ×2

contextmenu ×1

menu ×1

swiftui ×1

swiftui-list ×1