iOS 给App添加3DTouch功能

给App添加3DTouch的多个Item

//在didFinishLaunchingWithOptions方法中调用即可
- (void)add3DTouchItems:(UIApplication *)application{
    //给App图标添加3D Touch菜单
    //拍照
    
    //菜单图标
    UIApplicationShortcutIcon *iconCamera = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    //菜单文字
    UIMutableApplicationShortcutItem *itemCamera = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"拍照"];
    //绑定信息到指定菜单
    itemCamera.icon = iconCamera;
    
    //相册
    //菜单图标
    UIApplicationShortcutIcon *iconPhotoLibrary = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    //菜单文字
    UIMutableApplicationShortcutItem *itemPhotoLibrary = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"相册"];
    //绑定信息到指定菜单
    itemPhotoLibrary.icon = iconPhotoLibrary;
    
    UIMutableApplicationShortcutItem *openItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"3" localizedTitle:@"打开"];
    openItem.localizedSubtitle = @"文件详情";
    openItem.icon = nil;
    //绑定到App icon
    application.shortcutItems = @[itemCamera,itemPhotoLibrary,openItem];
    
}
//UIApplicationDelegate 在这里处理点击的item
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
    NSLog(@"UIApplicationShortcutItem == %@",shortcutItem);
}

给某个页面添加3DTouch

3DTouch添加页面

#import "ThreeDTouchVC.h"
#import "HqPreviewVC.h"

@interface ThreeDTouchVC ()
{
    HqPreviewVC *vc;
}
@end

@implementation ThreeDTouchVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
    UILabel *lab = [[UILabel alloc] init];
    lab.bounds = CGRectMake(0, 0, 200, 20);
    lab.text = @"3DTouch-长按屏幕";
    lab.center = self.view.center;
    [self.view addSubview:lab];
    vc = [[HqPreviewVC alloc] init];
    [self check3DTouch];

}
//检测页面是否处于3DTouch
- (void)check3DTouch{
    
    if ([self respondsToSelector:@selector(traitCollection)]) {
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            
            [self registerForPreviewingWithDelegate:self sourceView:self.view];
            NSLog(@"3D Touch 开启");
        }
    }
}


#pragma mark - UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location{
    NSLog(@"viewControllerForLocation");
    

    return vc;
}
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    NSLog(@"commitViewController===");
    ///在预览UIViewController界面继续按压进入预览UIViewController
    [self showViewController:viewControllerToCommit sender:self];

}

预览页面

#import "HqPreviewVC.h"

@interface HqPreviewVC ()

@end

@implementation HqPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc
                                 ] initWithTarget:self action:@selector(tap)] ;
    [self.view addGestureRecognizer:tap];
    
}
- (void)tap{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UIPreviewActionItem
- (NSArray> *)previewActionItems{
    UIPreviewAction *action1 =[UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击了分享");
    }];
    
    UIPreviewAction *action2 =[UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击了收藏");
    }];
    
    NSArray *actions = @[action1,action2];
    return actions;
}

你可能感兴趣的:(iOS 给App添加3DTouch功能)