我的应用程序上有一个 UIMenu,我想检测用户何时在外部点击(关闭)UIMenu。但是默认情况下,Apple 似乎不支持此操作的任何代表。因为当用户在外面点击时,我想更改按钮的图像。 示例图像
1 回答
1
我发现的一种相当老套的方法是子类化UIButton
和覆盖contextMenuInteractionWillEndFor
.
class MyButton: UIButton {
override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willEndFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
super.contextMenuInteraction(interaction, willEndFor: configuration, animator: animator)
print("ending!")
}
}
contextMenuInteractionWillEndFor
是当您关闭 a 时调用的委托方法UIMenu
,但是如果您正在设置按钮的menu
属性,则按钮本身将成为 的委托UIContextMenuInteraction
,并且您不能将其设置为其他东西,这就是您必须子类化的原因UIButton
。
将此与使用添加上下文菜单时进行比较addInteraction
,您可以控制UIContextMenuInteractionDelegate
.
于 2022-01-27T08:59:40.630 回答