美文网首页
iOS9 新特征之3D Touch实现

iOS9 新特征之3D Touch实现

作者: 雄壮的玻璃心 | 来源:发表于2015-12-16 15:10 被阅读0次

    API介绍

    UITouch类里API的变化

    iOS9中添加的属性

    altitudeAngle

    • 当笔平行于平面时,该值为0
    • 当笔垂直于平面时,该值为Pi / 2

    estimatedProperties

    • 当前触摸对象估计的触摸特性,返回值是UITouchPropertyies

    updatedProperties

    • 当前触摸对象已经更新的触摸特性,返回值是UITouchPropertyies

    estimationUpdateIndex

    • 当每个触摸对象的触摸特性发生变化时,该值将会单独增加,返回值是NSNumber

    iOS9中添加的方法

    • PreciseLocationInView:当前触摸对象的坐标
    • PrecisePreviousLocationInView:当前触摸对象的前置坐标
    • azimuthAngleInview:沿着x轴正向的方位角,当与x轴正向方向相同时,该值为0;当view参数为nil时,默认为keyWindow
    • azimuthUnitVectorInView:当前触摸对象的方向上的单位向量
      当view参数为nil时,默认为keyWindow
    UIForceTouchCapability
    • UIForceTouchCapabilityUnknown 不能确定是否支持压力感应
    • UIForceTouchCapabilityUnavailable 不能支持压力感应
    • UIForceTouchCapabilityAvailable 可以支持压力感应

    UITouchType

    • UITouchTypeDirect 垂直的触摸类型
    • UITouchTypeIndirect 非初值的触摸类型
    • UITouchTypeStylus 水平的触摸类型

    关于重按App icon展开菜单的实现

    在项目的Appdelegate中找到如下方法,创建展开菜单

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    for example
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window makeKeyAndVisible];
        UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
        self.window.rootViewController = vc;
    
        UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"item1" localizedTitle:@"我的消息" localizedSubtitle:nil icon:nil userInfo:nil];
        
        UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
        UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"item2" localizedTitle:@"发布动态" localizedSubtitle:nil icon:icon2 userInfo:nil];
        
        UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ico_discovery"];
        UIApplicationShortcutItem *item3= [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"搜索寻味师" localizedSubtitle:nil icon:icon3 userInfo:nil];
        application.shortcutItems = @[item3,item2,item1];
        
        
    }
    

    创建完之后就运行项目,看到效果啦,如图

    image

    之后可以在application:performActionForShortcutItem:completionHandler方法内根据shortcutItem.type处理点击事件

    -(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

    for example
    if ([shortcutItem.type isEqualToString:@"item1"]) {    
        MyMessageViewController *vc = [[MyMessageViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController.tabBarController.selectedViewController pushViewController:vc animated:YES];
    }
    

    }

    关于peek 和 pop的实现

    效果图
    image

    首先创建2个viewController,例如HomeViewControllerDetailPageViewController

    • HomeViewController:需要添加peek和pop的ViewController
    • DetailPageViewController:要展示的View Controller

    在HomeViewController中添加<UIViewControllerPreviewingDelegate>,并注册delegate

    [self registerForPreviewingWithDelegate:self sourceView:self.view];

    实现该Delegate的方法

    -(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)context viewControllerForLocation:(CGPoint)location
    {

    CGPoint point = [self.view convertPoint:location toView:self.tableView];//根据loaction获取其在不同坐标系中的点坐标,后面会通过这个取获取数据的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    CGRect cellRect = cell.frame;
    context.sourceRect = cellRect;
    Humors *humor = [(FinderInfo*)dataList[indexPath.row] humors][0];//这是我们项目的一个model,大家可以换成自己的model进行操作
        DetailPageViewController *controller = [[DetailPageViewController alloc]initWithFlavorId:[humor.param intValue]];//创建需要pop的ViewController        
        controller.view.frame = self.view.frame;
        return controller;
    

    }

    -(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{

    [self showViewController:viewControllerToCommit sender:self];
    

    }

    效果图
    image

    然后在DetailPageViewController中实现peek操作

    打开前面pop出的ViewController(DetailPageViewController),在实现文件中添加如下代码

    - (NSArray<id<UIPreviewActionItem>> *)previewActionItems{

    __weak typeof(self) weakSelf = self;
    NSString *actionTitle = mUserRes.isAttention?@"取消关注":@"+关注";
    UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:actionTitle style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        [weakSelf attention]; //这里添加你需要点击后的操作
    }];
    
    
    UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //这里添加你需要点击后的操作
    }];
    NSArray *actions = @[p1,p2];
    return actions;
    

    }

    Tips:判断设备是否支持3D touch,需要做判断
    • self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable
    • [[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0f

    必须要两样同时满足,才可以使用,不作判断会引起不支持3D Touch的手机编译crash

    部分内容摘自简书原地址点我

    相关文章

      网友评论

          本文标题:iOS9 新特征之3D Touch实现

          本文链接:https://www.haomeiwen.com/subject/wgzuhttx.html