3D Touch的应用

3D Touch的应用在App中主要分为三个模块。
一、Quick Actions
通过按压主屏幕的应用图片打开应用。

  • 定义功能菜单。功能菜单有两种定义方式,静态在.plist文件中保存,或者动态通过方法创建。


    静态创建ShortcutItem.png
// 动态创建
- (void)creatShortcutItem {
    //创建系统风格的icon
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];

    UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"UITouchText.share" localizedTitle:@"分享" localizedSubtitle:@"分享副标题" icon:icon userInfo:nil];
    
    // 添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[item];
}

其中,有两个必须设置的值~
UIApplicationShortcutItemType 字符串标示,区分用户的点击
UIApplicationShortcutItemTitle 字符串,用于屏幕显示
其他的副标题、图标之类的,看需要设置。

  • 在AppDelegate文件中实现方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { }

在方法中判断shortcutItem的type的不同来执行不同操作。

二、Peek and Pop
按压预览及进入的需求~实现UIViewControllerPreviewingDelegate协议,协议中一共有两个方法:

NS_CLASS_AVAILABLE_IOS(9_0) @protocol UIViewControllerPreviewingDelegate <NSObject>

// If you return nil, a preview presentation will not be performed
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0);
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0);

@end
  • 设置预览的视图
// 预览,返回预览视图
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    // 获取按压的cell所在行,[previewingContext sourceView]就是按压的那个视图
    NSIndexPath *indexPath = [_tableView indexPathForCell: (UITableViewCell *)[previewingContext sourceView]];
    
    // 调整不被虚化的范围,按压的那个cell不被虚化
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, 40);
    previewingContext.sourceRect = rect;
    
    // 设定预览的界面,preferredContentSize决定了预览视图的大小
    ...
    previewingVC.preferredContentSize = CGSizeMake(0.0f, 500.0f);
    
    return previewingVC;
}

值得注意的是:想要响应 3D touch 的 view 需进行注册,在我这里,响应 3D Touch 的 view 是 cell(感谢 旭丶Joy 的提醒):

//  判断3DTouch是否可用
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0 &&
            self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        //  注册响应 3D Touch 的 View
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else {
        NSLog(@"3D Touch 不可用");
    }
  • 用力按压进入页面
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    
    [self showViewController:viewControllerToCommit sender:self];
}

预览时上移页面底部有对应操作按钮,这个在对应的页面.m文件里添加方法即可~

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

三、Force Properties
UITouch新增force属性,可通过检测压力不同并且用CGFloat类型表示。实现并不复杂。

这个我在这次的项目中似乎用不上🤔。

**好了说完了。 **

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

推荐阅读更多精彩内容

  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 4,539评论 8 18
  • 1.什么是3D Touch? 3D Touch属于一种人机交互的一种方式,具体点用户与手机屏幕的一种交互方式。在3...
    alpha_feng阅读 792评论 0 3
  • 专著://www.greatytc.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 1,029评论 1 0
  • 1.简单的介绍一下3D Touch 3D Touch的触控技术,被苹果称为新一代多点触控技术。其实,就是此前在Ap...
    Camille_chen阅读 12,087评论 19 33
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,866评论 25 708