美文网首页
iOS 3DTouch

iOS 3DTouch

作者: 开心的小娇 | 来源:发表于2019-04-02 17:48 被阅读0次

    1.如果想要让自己的项目是想3dTouch效果,只需要在Appdele里面添加如下代码即可:

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

        //这里可以获的shortcutItem对象的唯一标识符

        //不管APP在后台还是进程被杀死,只要通过主屏快捷操作进来的,都会调用这个方法

        NSLog(@"name:%@\ntype:%@", shortcutItem.localizedTitle, shortcutItem.type);

    }

    - (void)initShortcutItems {

        if ([UIApplication sharedApplication].shortcutItems.count >= 4)

            return;

        NSMutableArray *arrShortcutItem = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;

        UIApplicationShortcutItem *shoreItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"cn.damon.DM3DTouchDemo.openSearch" localizedTitle:@"搜索" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch] userInfo:nil];

        [arrShortcutItemaddObject:shoreItem1];

        UIApplicationShortcutItem *shoreItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"cn.damon.DM3DTouchDemo.openCompose" localizedTitle:@"新消息" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose] userInfo:nil];

        [arrShortcutItemaddObject:shoreItem2];

       [UIApplication sharedApplication].shortcutItems = arrShortcutItem;

        NSLog(@"%lu", [UIApplication sharedApplication].shortcutItems.count);

    }

    2.还有一种就是类似于微信使用3DTouch功能点击会话列表功能,这种功能稍微复杂点,需要首先实现tableView的数据源的一个方法,如下:

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId];

        if(!cell) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];

        }

        cell.contentView.backgroundColor = [UIColor whiteColor];

        NSString *str = [NSString stringWithFormat:@"row [%@]", self.arrData[indexPath.row]];

        cell.textLabel.text= str;

        //注册3D Touch

        //只有在6s及其以上的设备才支持3D Touch,我们可以通过UITraitCollection这个类的UITraitEnvironment协议属性来判断

        /**

         UITraitCollection是UIViewController所遵守的其中一个协议,不仅包含了UI界面环境特征,而且包含了3D Touch的特征描述。

         从iOS9开始,我们可以通过这个类来判断运行程序对应的设备是否支持3D Touch功能。

         UIForceTouchCapabilityUnknown = 0,    //未知

         UIForceTouchCapabilityUnavailable = 1, //不可用

         UIForceTouchCapabilityAvailable = 2    //可用

         */

        if([self  respondsToSelector:@selector(traitCollection)]) {

                 if([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {

                if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

                    [self registerForPreviewingWithDelegate:(id)self sourceView:cell];

               }

            }

       }

       return cell;

    }

    然后自定义一个类,并在类中实现相关方法:

    #pragma mark - 3D Touch 预览Action代理

    - (NSArray> *)previewActionItems {

        NSMutableArray *arrItem = [NSMutableArray array];

        UIPreviewAction *previewAction0 = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

            NSLog(@"didClickCancel");

        }];

        UIPreviewAction *previewAction1 = [UIPreviewAction actionWithTitle:@"替换该元素" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

            //把下标为index的元素替换成preview

            [selfreplaceItem];

        }];

        [arrItemaddObjectsFromArray:@[previewAction0 ,previewAction1]];

        returnarrItem;

    }

    - (void)replaceItem {

        if(self.arrData.count<=0)return;

        [self.arrData replaceObjectAtIndex:self.index withObject:@"replace  item"];

    }

    代码地址:git地址,欢迎大家指点

    相关文章

      网友评论

          本文标题:iOS 3DTouch

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