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

后端 未结 3 1198
执念已碎
执念已碎 2020-12-30 11:10

Phone, Reminders, and Maps use a different 3D-Touch Peek UI allowing to select an action in one go. For instance, force-press on a reminder and select \"Remind me on a day\"

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 11:24

    Those are UIPreviewActionItem.

    After overriding previewingContext:viewControllerForLocation: you can also override - (NSArray> *)previewActionItems and that will allow you to specify your quick actions.

    Here's a snippet that will help you out: (related tutorial)

    - (NSArray> *)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 Docs:

    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.

提交回复
热议问题