微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在快速点击按钮时显示上下文菜单

在 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])
       }
    }

解决方法

UIContextMenuInteraction 仅用于上下文(长按)菜单。

如果你想让按钮的主要动作显示一个菜单,你可以创建一个UIMenu并将它直接分配给button.menu属性,然后设置button.showsMenuAsPrimaryAction = true,像这样:

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

...

let button = UIButton()
button.showsMenuAsPrimaryAction = true
button.menu = UIMenu(title: "",children: [favorite,...])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。