3DTouch的简单使用

3DTouch的简单使用

pop & peek

效果图

运用场景

常见的就是新闻的列表,或者即时通讯的聊天列表等等,可以给用户比较好的交互体验,上滑出现一些操作按钮,比如收藏,置顶,点赞等等便捷操作.

代码实现
  • 1.首先我们在listVC页遵循代理:
  • 2.我们需要给视图注册peek以及pop的功能: 这边有2种不同的注册:(a) 给我们点击的cell 注册 (b) 给tableview注册,然后根据位置信息找到我们点击的cell 都行,但是给cell注册的时候我们肯定是在cellForRow的代理的方法中,这样tableview在不断滑动的时候,反复注册,比较影响性能,所以我们这边选择(b)
  - (void)viewDidLoad { 
    if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0&&self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        //如果支持,给tableview注册3DTouch的peek(预览)和pop功能
        [self registerForPreviewingWithDelegate:self sourceView:self.tableView];
    }
}
复制代码

3.实现代理:

#pragma mark - UIViewControllerPreviewingDelegate
// 预览效果
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    // 获取按压的那个视图 , 也是你注册的那个视图
    UITableView *tableview = (UITableView *)[previewingContext sourceView];
    // 根据CGPoint获取你按压的NSIndexPath,再获取你的cell
    NSIndexPath *indexPath = [tableview indexPathForRowAtPoint:location];
    UITableViewCell *touchCell = [tableview cellForRowAtIndexPath:indexPath];
    LKCollectListModel *model = self.dataListArr[indexPath.row];
    NSString *status = [model.status componentsSeparatedByString:@"|"].firstObject;
    DetailVC *vc = [DetailVC new];
    vc.collectno = model.infocollectno;
    vc.preferredContentSize = CGSizeMake(0, 500);
    //调整不被虚化的范围,按压的那个cell不被虚化(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)
    CGRect rect = touchCell.frame;
    previewingContext.sourceRect = rect;
    return vc;
}

// 用力按进入
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}
复制代码

// 有的时候有上滑出现按钮的效果 , 这个需要在详情页设置:

#pragma mark - UIPreviewActionItem
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    NSMutableArray *arrItem = [NSMutableArray array];
    UIPreviewAction *previewAction0 = [UIPreviewAction actionWithTitle:@"查看流程" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        // 发送通知:让listVC去做一些事情
        [[NSNotificationCenter defaultCenter] postNotificationName:@"headerRefersh" object:self userInfo:nil];

    }];
    [arrItem addObjectsFromArray:@[previewAction0]];
    return arrItem;
}

复制代码
存在的问题
问题: 出现在详情页创建按钮 **UIPreviewAction** 的响应的时候 : previewViewController 是当前的详情页,但是它无法进行跳转  比如我这边的需求是跳转到 一个流程历史页面 无法实现 ,无论push 还是 present
     处理方法: 发送通知让列表页面去做事情,比如刷新什么的操作
     理解: 可能苹果不希望使用这么复杂的需求,这些item的存在是进行一些方便操作的,比如收藏,置顶,这些简易操作,而不是用到页面间的操作
复制代码

Appicon 按压

效果图

代码实现

在Appdeleagte中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0){
    // 系统的枚举提供了许多样式,也可以用iconWithTemplateImageName设置自定义图标
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMarkLocation];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"newType" localizedTitle:@"新增功能" localizedSubtitle:nil icon:icon1 userInfo:nil];
    if(item1){
       application.shortcutItems = @[item1];
    }
    // 应用上线AppStore之后系统会给应用自动生成一个分享功能
    }
    return YES;
}
复制代码

// 通过图标的3D touch功能进入时,走这个方法:

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    // 通过type去区分:
    if ([shortcutItem.type isEqualToString:@"newType"]) {
        // 路由处理 : 看具体需求
    }
}
复制代码

结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值