iOS 14的UIMenu和ContextMenu中的新增功能

UIMenu was introduced in iOS 13 to allow easy creation of menus and submenus, which is really handy when building context menus.

UIMenu是iOS 13中引入的,可轻松创建菜单和子菜单,这在构建上下文菜单时非常方便。

iOS 14 brings more customization to UIMenu by introducing out-of-the-box support for UIButton and UIBarButtonItem through the menu property and initializers respectively.

iOS 14通过分别通过menu属性和初始值设定项引入了对UIButtonUIBarButtonItem的现成支持,为UIMenu带来了更多自定义。

In the following sections, we’ll look at how to set the menu on the above-mentioned UI controls. We’ll also look at the new UIDeferredMenuElement that lets us create dynamic menu items.

在以下各节中,我们将研究如何在上述UI控件上设置菜单。 我们还将查看新的UIDeferredMenuElement ,它使我们可以创建动态菜单项。

在UIButton上添加UIMenu (Adding UIMenu on UIButton)

By assigning the UIMenu to the new menu property on UIButton, UIKit automatically takes care of displaying that menu on a long press.

通过指定UIMenumenu上的财产UIButton ,UIKit的自动处理上长按显示该菜单。

Notably, setting the displayInline as the option displays the submenu within the parent menu itself.

值得注意的是,将displayInline设置为option会在父菜单本身内显示子菜单。

An image where the button press shows menu on long press because showAsPrimaryAction is set to false.

To show the menu immediately on button tap, we need to set the showsMenuAsPrimaryAction property to true:

要在单击按钮时立即显示菜单,我们需要将showsMenuAsPrimaryAction属性设置为true

button.showsMenuAsPrimaryAction = true

In order to determine if the menu is triggered and perhaps perform some other action, iOS 14 has introduced a new control event for UIButton, namely, menuActionTriggered. We can set it to fire the UIAction handler in the following way:

为了确定菜单是否被触发并可能执行其他操作,iOS 14为UIButton引入了一个新的控制事件,即menuActionTriggered 。 我们可以通过以下方式将其设置为触发UIAction处理程序:

button.addAction(UIAction(title: ""){ _ in print("Hello Menu")},for: .menuActionTriggered)

在UIBarButtonItem上添加UIMenu (Adding UIMenu On UIBarButtonItem)

Besides providing the menu property to create and display UI menus, the UIBarButtonItem also introduces an initializer wherein you can pass the UIMenu group and primary actions as optional arguments.

除了提供用于创建和显示UI菜单的menu属性之外, UIBarButtonItem还引入了一个初始化程序,您可以在其中传递UIMenu组和主要操作作为可选参数。

The following code shows how to construct UIBarButtonItems with primaryAction and menu initialisers:

以下代码显示了如何使用primaryActionmenu初始化程序构造UIBarButtonItems

The important difference between UIButton and UIBarButtonItem menu action is that, for the latter, there is no showAsPrimaryAction property. So, if you want the UIBarButtonItem to display the menu immediately on touchdown, don’t set a primaryAction.

UIButtonUIBarButtonItem菜单操作之间的重要区别在于,对于后者,没有showAsPrimaryAction属性。 因此,如果您希望UIBarButtonItem在触地时立即显示菜单,请不要设置primaryAction

Image for post

iOS 14 UIDeferredMenuElement (iOS 14 UIDeferredMenuElement)

UIDeferredMenuElement is a powerful new component introduced in iOS 14 that lets you add menu items in an asynchronous fashion.

UIDeferredMenuElement是iOS 14中引入的功能强大的新组件,可让您以异步方式添加菜单项。

In order to so, you provide a standard placeholder menu that gets replaced with the deferred menu elements once it’s loaded.

为此,您提供了一个标准的占位符菜单,该菜单在加载后将被延迟​​的菜单元素替换。

Deferred menus provide a built-in caching mechanism that ensures that subsequent loads of the same instance are faster.

延迟菜单提供了一种内置的缓存机制,可确保同一实例的后续加载更快。

Image for post

UIDeferredMenuElement is also useful for generating deeply nested menus where the items won’t be displayed until they’re needed.

UIDeferredMenuElement还可用于生成深层嵌套的菜单,在这些菜单中,直到需要时才显示这些项目。

iOS 14新的ContextMenu交互 (iOS 14 New ContextMenu Interactions)

Context menu interactions were introduced last year as a replacement for the 3D touch menus. iOS 14 bumps it up with two new features:

去年引入了上下文菜单交互,以替代3D触摸菜单。 iOS 14具有两项新功能:

  • updateVisibleMenuUIMenu elements are no longer immutable, thereby allowing you to replace their attributes in place, while they’re being displayed. This will certainly help in toggling states within context menus.

    updateVisibleMenuUIMenu元素不再是不可变的,因此允许您在显示它们时替换它们的属性。 当然,这将有助于在上下文菜单中切换状态。

  • Customizing appearances by invoking the menuAppearance property on UIContextMenuIteraction instance. This new property lets you set the appearance of the context menu as rich or compact, with the former being for displaying previews and the latter being the default look that we’ve seen in the previous sections.

    通过调用UIContextMenuIteraction实例上的menuAppearance属性来定制外观。 这个新属性使您可以将上下文菜单的外观设置为richcompact ,前者用于显示预览,而后者则是我们在上一节中看到的默认外观。

总结思想 (Closing Thoughts)

The revamped UIMenus will now be supported on Apple’s own applications for most UI controls. For instance, the options button, on the toolbar in the Safari app, now displays the above compact context menu instead of the old UI action sheet.

Apple自己的应用程序现在将为大多数UI控件支持经过改进的UIMenu 。 例如,Safari应用程序工具栏中的选项按钮现在显示上面的紧凑上下文菜单,而不是旧的UI操作表。

The UIMenus will help perform rapid navigation from the back button as well.

UIMenu还将有助于从后退按钮执行快速导航。

The updates Apple has brought to UIKit this year clearly showcase that the framework is here to stay. For a few more years at least.

苹果今年向UIKit带来的更新清楚地表明了该框架将继续存在。 至少再过几年。

That’s it for this one. Thanks for reading.

就这个。 谢谢阅读。

翻译自: https://medium.com/better-programming/whats-new-in-ios-14s-uimenu-and-contextmenu-433cd2037c37

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值