swift使用菜单_使用UIContextMenuInteraction在Swift中创建调试菜单

swift使用菜单

Debug menus in iOS are a very effective way to make your day more productive as a developer. If you find yourself doing the same debugging tasks over and over, such as printing back-end responses, skipping to specific view controllers, or copying user identifiers, it’s nice to have special developer menus in your app that automatically handle these tasks for you.

iOS中的调试菜单是提高开发人员工作效率的一种非常有效的方法。 如果您发现自己一遍又一遍地执行相同的调试任务,例如打印后端响应,跳到特定的视图控制器或复制用户标识符,那么很高兴在您的应用程序中包含特殊的开发人员菜单,这些菜单会自动为您处理这些任务。

iOS 13 introduced UIContextMenuInteraction, a new context menu API that replaces the older (and much harder to use) peek-and-pop menus. When a UIView with a registered interaction is pressed, a menu containing actions and an optional preview will show up. It works similarly to an UIAlertController, but it’s nicer to look at and much easier to implement! In iOS 14, we can even add asynchronously resolved actions to it.

iOS 13引入了UIContextMenuInteraction ,这是一个新的上下文菜单API,它取代了较旧的(且UIContextMenuInteraction使用)窥视和弹出菜单。 当按下具有已注册交互的UIView ,将显示一个包含动作和可选预览的菜单。 它的工作方式类似于UIAlertController ,但看起来更好,更易于实现! 在iOS 14中,我们甚至可以向其中添加异步解析的操作。

Image for post

Because these context menus are implemented directly into the view (as opposed to something like an alert, which is an independent view controller), we can create an abstraction that implements debug menus to specific views if we’re running a developer build.

因为这些上下文菜单是直接在视图中实现的(与诸如警报(它是一个独立的视图控制器)之类的东西相对),所以我们可以创建一个抽象,如果我们正在运行开发人员版本,则该抽象可对特定视图实现调试菜单。

示例:将已记录的用户数据复制到粘贴板上 (Example: Copy a Logged User’s Data to the Pasteboard)

In this example, let’s pretend that we have an app where we often have to retrieve the logged account’s user identifier. In past projects, I had to do this a lot for debugging and bug reporting purposes — either to let the back end know which user I was having a problem with or to be able to manually send back-end requests in a service like Postman.

在此示例中,我们假设我们有一个应用程序,在该应用程序中我们经常不得不检索已登录帐户的用户标识符。 在过去的项目中,出于调试和错误报告的目的,我不得不做很多事情-要么让后端知道我遇到了哪个用户,要么可以在Postman之类的服务中手动发送后端请求。

I normally did that by setting a breakpoint and printing the user’s model fields, which was a very slow and annoying process. Let’s add a special debug menu that does this for us.

我通常通过设置一个断点并打印用户的模型字段来做到这一点,这是一个非常缓慢而烦人的过程。 让我们添加一个特殊的调试菜单来为我们执行此操作。

UIContextMenuInteraction works through a delegate. To have a debug menu show up for a view, you must inherit the delegate, define a list of UIActions that should show up, and insert the interaction in the view. In this case, we'll add the interaction directly in the initializer.

UIContextMenuInteraction通过委托工作。 要显示视图的调试菜单,您必须继承委托,定义应显示的UIActions列表,然后在视图中插入交互。 在这种情况下,我们将直接在初始化程序中添加交互。

Here’s how our “debuggable user view” looks:

这是我们的“可疑用户视图”的外观:

Image for post

There are a few ways to remove this code from release builds, and my recommended way is to use preprocessor macros to completely eliminate the “debug code.” Other approaches will still allow your code to be reverse-engineered by hackers.

有几种方法可以从发行版本中删除此代码,而我建议的方法是使用预处理程序宏来完全消除“调试代码”。 其他方法仍将允许您的代码被黑客逆向工程。

#if DEBUG
extension UserTextField: UIContextMenuInteractionDelegate {
...
#endif

针对应用程序中的每个UIView进行扩展 (Expanding It for Every UIView in the App)

We have created a debug menu for a single view, but what about the others? As you can see, this code doesn’t look very nice and will get difficult to maintain pretty quickly. To remedy this, we can subclass UIContextMenuInteraction into a cleaner abstraction that only requires the list of actions to display. No configurations, previews, or other annoying things.

我们已经为单个视图创建了调试菜单,但是其他视图又如何呢? 如您所见,此代码看起来不太好,并且很难快速维护。 为了解决这个问题,我们可以将UIContextMenuInteraction子类化为更UIContextMenuInteraction的抽象,该抽象只需要显示动作列表即可。 没有配置,预览或其他烦人的事情。

I chose to create DebugMenuInteraction— a special UIContextMenuInteraction that handles its own delegate, exposing a DebugMenuInteractionDelegate instead to retrieve the list of debugging actions:

我选择创建DebugMenuInteraction一个特殊的UIContextMenuInteraction ,用于处理其自己的委托,而公开DebugMenuInteractionDelegate来检索调试操作的列表:

This allows us to refactor UserTextField into a much cleaner structure:

这使我们能够将UserTextField重构为更UserTextField结构:

Previously, the interaction was being added directly to the class’s initializer. This can still be done, but to allow us to better separate the “debug code” from the production code, we can create a global extension instead:

以前,交互是直接添加到类的初始化程序中的。 仍然可以这样做,但是为了让我们更好地将“调试代码”与生产代码分开,我们可以创建一个全局扩展:

Since the extension itself is able to determine if we’re running a debug build, calls to addDebugMenuInteraction() can be kept in production code, as the compiler will automatically optimize it out of the build.

由于扩展本身可以确定我们是否正在运行调试版本,因此对addDebugMenuInteraction()调用可以保留在生产代码中,因为编译器会自动从构建中对其进行优化。

With these abstractions, the final result is the same, but the code is now easier to maintain and evolve, as supporting different views is just a matter of extending them to conform to the new delegate.

使用这些抽象,最终结果是相同的,但是代码现在更易于维护和发展,因为支持不同的视图只是扩展它们以使其符合新委托的问题。

iOS 14 —具有UIDeferredMenuElement的异步操作 (iOS 14 — Asynchronous Actions With UIDeferredMenuElement)

The arrival of iOS 14 introduced the possibility of creating menu elements that are resolved asynchronously. If your debug actions involve some data that requires an API call, you can use the new APIs to create async actions that are automatically displayed in the UIContextMenuInteraction with a special loading UI:

iOS 14的到来引入了创建异步解决的菜单元素的可能性。 如果您的调试操作涉及一些需要API调用的数据,则可以使用新的API创建异步操作,这些操作将通过特殊的加载UI自动显示在UIContextMenuInteraction

The completion type of the deferred action is an array of UIMenuElements, meaning that a single action can be resolved to multiple entries in the debug menu. When displayed, you'll get a nice loading screen that is replaced with the real actions once the completion handler is called.

延迟动作的完成类型是UIMenuElements的数组,这意味着可以将单个动作解析为调试菜单中的多个条目。 当显示时,您将获得一个漂亮的加载屏幕,一旦完成处理程序被调用,该屏幕就会被实际操作替换。

Image for post

结论 (Conclusion)

Thanks for reading. If you’re interested in creating debug menus, the code we created in this article is available in SPM and CocoaPods as the DebugActions library.

谢谢阅读。 如果您对创建调试菜单感兴趣,可以在SPM和CocoaPods中将本文中创建的代码作为DebugActions库。

翻译自: https://medium.com/better-programming/create-debug-menus-in-swift-with-uicontextmenuinteraction-e1718c73672c

swift使用菜单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值