• iOS开发--3D Touch的基本使用



    1.桌面快捷菜单项

    效果如图:


    桌面快捷菜单


    点击之后的效果如图:


    点击桌面快捷菜单的效果

    接下来看下具体实现:
    1).在-application:didFinishLaunchingWithOptions:
    方法中用-setShortcutItems:方法来添加快捷菜单项。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //3D Touch按压程序图标的快捷项
    //快捷菜单的图标
    UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
    UIApplicationShortcutIcon *icon2=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutIcon *icon3=[UIApplicationShortcutIcon iconWithTemplateImageName:@"search"];
    //快捷菜单
    UIApplicationShortcutItem *item1=[[UIApplicationShortcutItem alloc]initWithType:@"1"
    localizedTitle:@"嘿嘿"
    localizedSubtitle:nil
    icon:icon1
    userInfo:nil];
    UIApplicationShortcutItem *item2=[[UIApplicationShortcutItem alloc]initWithType:@"1"
    localizedTitle:@"呵呵"
    localizedSubtitle:@"干嘛去洗澡"
    icon:icon2
    userInfo:nil];
    UIApplicationShortcutItem *item3=[[UIApplicationShortcutItem alloc]initWithType:@"1"
    localizedTitle:@"搜索"
    localizedSubtitle:nil
    icon:icon3
    userInfo:nil];
    //设置app的快捷菜单
    [[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3]];
    //导航
    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[ViewController new]];
    return YES;
    }

    2).在-application:performActionForShortcutItem:completionHandler:方法中实现点击快捷菜单的方法:
    //3D Touch按压程序图标的快捷项时触发的方法
    -(void)application:(UIApplication )application performActionForShortcutItem:(UIApplicationShortcutItem )shortcutItem completionHandler:(void (^)(BOOL))completionHandler
    {
    NSString title;
    if([shortcutItem.localizedTitle isEqualToString:@"嘿嘿"])
    {
    title=@"嘿嘿";
    }
    else if([shortcutItem.localizedTitle isEqualToString:@"呵呵"])
    {
    title=@"呵呵";
    }
    else if([shortcutItem.localizedTitle isEqualToString:@"搜索"])
    {
    title=@"搜索";
    }
    //这里就弹个框子意思一下
    //由于UIAlertView在iOS 9被废弃,因此选用UIAlertController
    UIAlertController
    alertController=[UIAlertController alertControllerWithTitle:@"提示"
    message:[NSString stringWithFormat:@"你点击了“%@”",title]
    preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction action=[UIAlertAction actionWithTitle:@"知道了"
    style:UIAlertActionStyleDefault
    handler:^(UIAlertAction
    action) {
    [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:action];
    [self.window.rootViewController presentViewController:alertController
    animated:YES
    completion:nil];
    }

    2.程序内的3DTouch按压操作:


    表视图

    3DTouch按压的时候生成的预览

    peek时上拉出现的菜单


    1).首先,实现3DTouch的视图控制器要遵守:
    UIViewControllerPreviewingDelegate协议,它有2个required级别的协议方法:
    -previewingContext:viewControllerForLocation:
    以及
    -previewingContext:commitViewController:

    2).检测3DTouch是否可用,并注册3DTouch:

    检测3D Touch是否可用

    -(BOOL)is3DTouchAvailiable
    {
    if(self.traitCollection.forceTouchCapability==UIForceTouchCapabilityAvailable)
    return YES;
    return NO;
    }

    注册3DTouch
    if([self is3DTouchAvailiable])
    {
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }

    3).实现协议方法:

    -(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
    {
    NSIndexPath *indexPath=[_tbVew indexPathForRowAtPoint:CGPointMake(location.x, location.y-64)];
    if(indexPath)
    {
    DetailViewController *detail=[[DetailViewController alloc]init];
    detail.title=_dataArray[indexPath.row];
    //detail.preferredContentSize=CGSizeMake(300, 300);
    __weak typeof(self) wkSelf=self;
    //------------上拉时的菜单-------------------
    //置顶及其点击逻辑
    UIPreviewAction *topAction=[UIPreviewAction actionWithTitle:@"置顶" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * action, UIViewController * previewViewController) {
    [wkSelf.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
    [wkSelf.tbVew reloadData];
    [wkSelf showAlert:@"提示" body:@"已置顶"];
    }];
    //删除及其点击逻辑
    UIPreviewAction *deleteAction=[UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction *action, UIViewController * previewViewController) {
    [wkSelf.dataArray removeObjectAtIndex:indexPath.row];
    [wkSelf.tbVew reloadData];
    [wkSelf showAlert:@"警告" body:@"已删除"];
    }];
    //传递上拉菜单项给detail
    detail.actions=@[topAction,deleteAction];
    return detail;
    }
    return nil;
    }
    -(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
    {
    [self showViewController:viewControllerToCommit sender:self];
    }

    4).要previewing的视图控制器必须实现:
    -(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法,该方法定义了peek时上拉出来的菜单:

    /**peek时上拉出来的菜单*/
    -(NSArray<id<UIPreviewActionItem>> *)previewActionItems
    {
    return self.actions;
    }

    https://github.com/whj111/3D_Touch_Demo
  • 相关阅读:
    spring boot 在idea中实现热部署
    spring boot jar的生成
    mongodb windows 开机启动
    使用阿里云RDS
    net core 使用ef生成实体类(SqlServer)
    在window下搭建即时即用的hyperledger fabric 的环境
    NET实现谷歌OCR的使用记录(CLOUD VISION API)
    kali 系列学习12-使用Wifite破解无线网络
    kali 系列学习10-渗透攻击MySQL数据库服务、PostgreSQL数据库服务、Tomcat服务和PDF文件
    kali 系列学习09-Kali-linux设置ProxyChains
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/5314831.html
Copyright © 2020-2023  润新知