如何实现 3D-Touch Peek-and-select?

How to implement 3D-Touch Peek-and-select?

Phone、提醒事项和地图使用不同的 3D-Touch Peek UI 允许 select 一次操作。例如,在不松开手指的情况下,一次性按下提醒和 select "Remind me on a day"。它在视觉上也不同于使用 UIViewControllerPreviewing API 的标准 3D Touch 预览,因为它在左对齐文本旁边显示自定义图标。

我无法使用官方 API 找到执行此操作的方法。我是不是错过了什么或者这确实是私人的 API?

那些是UIPreviewActionItem

覆盖 previewingContext:viewControllerForLocation: 后,您还可以覆盖 - (NSArray<id<UIPreviewActionItem>> *)previewActionItems,这将允许您指定快速操作。

这里有一个片段可以帮助您解决问题:(related tutorial)

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 triggered");
    }];

    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Destructive Action" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Destructive Action triggered");
    }];

    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Selected Action" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Selected Action triggered");
    }];

    return @[action1, action2, action3];
}

Apple 文档:

This property is for use with a preview (peek) view controller which you present in your implementation of the previewingContext:viewControllerForLocation: delegate method..

Implement this method to provide quick actions for such a preview. When the user swipes upward on the preview, the system presents these quick action items in a sheet below the preview.

The default implementation of this method returns an empty array.

对于未来的读者,Apple 对这个问题的回答是:

Currently there is not public API to do these things. Please file bug reports if this is something you want to do in your app, and include specific details of what you're looking to do.

来源:Apple Developer Forum

我也在 Apple Developer Forums 上问了我的问题,并收到了 Apple 的回复:

Currently there is not public API to do these things. Please file bug reports if this is something you want to do in your app, and include specific details of what you're looking to do.

因此目前无法使用官方SDK。我提交了这个 enhancement request radar,如果你也需要这个,我鼓励你复制它!