让所有的开发者都能使用3D Touch

闲来没事,就看了看3D Touch,写了个Demo,下面就简单介绍下如何实现这些功能。
网上教程说要在info.plist文件写快捷选项标签,我试了下不写,发现功能依旧能够实现,说说我的做法吧。

快捷功能

1.创建快捷选项

在Appdelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加一个方法来创建3D Touch的快捷选项,即Demo的[self addShortcutItem]方法,实现如下:

//创建系统风格的icon
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon_2"];
    //创建快捷选项
    UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"NSLog-ok" localizedTitle:@"系统图标" localizedSubtitle:nil icon:icon1 userInfo:nil];
    
    UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem alloc]initWithType:@"NSLog-hello" localizedTitle:@"自定义图标" localizedSubtitle:nil icon:icon2 userInfo:nil];
    
    //添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[item1,item2];

2.实现快捷选项功能

依旧在Appdelegate.m文件中,添加系统方法

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    //根据不同标识执行不同操作
    if([shortcutItem.type isEqualToString:@"NSLog-ok"]){
        
        NSLog(@"ok");
    }
    if([shortcutItem.type isEqualToString:@"NSLog-hello"]){
        
        NSLog(@"hello");
    }
    if (completionHandler) {
        completionHandler(YES);
    }
}

这样,快捷键功能就实现了,是不是很简单〜

peek(预览)和pop(跳至预览界面)功能

1.注册peek和pop功能(遵循UIViewControllerPreviewingDelegate协议)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中增加注册代码:

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        //3D Touch可用,注册peek和pop功能
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else {
        //3D Touch 无效
    }

2.实现peek和pop功能

2.1.1添加peek功能
//peek(预览)
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //获取按压的cell所在行,[previewingContext sourceView]就是按压的那个cell
    NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    
    //预览的界面
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PeekAndPopViewController *peekVC = [storyboard instantiateViewControllerWithIdentifier:@"PeekAndPopViewController"];
    peekVC.name = [NSString stringWithFormat:@"你点的是第%ld个cell",indexPath.row];
    peekVC.preferredContentSize = CGSizeMake(0.0f,500.0f);//设置预览界面的Size
    
    //设置不被虚化的rect(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,60);
    previewingContext.sourceRect = rect;
    
    return peekVC;
}
2.1.2添加预览界面的ActionSheet
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    // setup a list of preview actions
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Aciton1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //fountion
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Aciton2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //fountion
    }];
    
    NSArray *actions = @[action1,action2];
    return actions;
}
2.2实现pop功能
//pop(用力按页面跳转)
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self showViewController:viewControllerToCommit sender:self];
}

在这里需要说明的一点是,跳转到预览页面,可用[self dismissViewControllerAnimated:YES completion:nil];方法返回

测算点击力度功能

//按住移动or压力值改变时的回调
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSArray *arrayTouch = [touches allObjects];
    
    UITouch *touch = (UITouch *)[arrayTouch lastObject];
    
    if (touch.view == self.view) {
        //显示压力值
        _pressureLabel.text = [NSString stringWithFormat:@"压力值:%f",touch.force];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    _pressureLabel.text = nil;
}

具体效果可以看Demo,Demo地址:3D Touch-Demo

版权声明:本文为 Crazy Steven 原创出品,欢迎转载,转载时请注明出处!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容