iOS开发-------3D Touch之Peek,Pop,UIPreviewAction

       上一篇博客说了,如何在创建以及响应ApplicationShortcutItem的方法,那么现在再来应用一下如何响应Peek、Pop操作,什么叫Peek操作呢,就是说当稍微重按得时候会出现一个预览视图,在此时可以上划出现快捷按钮,再按一下会弹到详细视图。


Demo:https://github.com/YRunIntoLove/3DTouchTest


对于Peek以及Pop操作,楼主也感谢下面的博客

iOS9 3D Touch 使用第二篇


首先为了能快速构建Demo,楼主用StoryBoard来创建控制器ViewController,以UITableView为例,如下



在此之上,是否支持3D Touch以及是否开启3D Touch方法如下

/**
 *  检测是否支持3D Touch
 *
 *  @return YES || NO
 */
- (BOOL)Check3DTouch
{
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        return YES;
    }
    
    return NO;
}


cell响应3D Touch,所以在UITableViewController的-tableView:cellForRowAtIndexPath:中将cell注册为响应的源视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = _data[indexPath.row];
    
#ifdef __IPHONE_9_0
    if ([self Check3DTouch])
    {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
#endif
    return cell;
}


响应3D Touch的控制器需要遵守协议<UIViewControllerPreviewingDelegate>,实现如下两个协议方法
#pragma mark -UIViewControllerPreviewing Delegate

//Peek代理方法
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //获得当前cell的indexPath
    NSIndexPath * index = [self.tableView indexPathForCell:(UITableViewCell *)previewingContext.sourceView];
    
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    TextViewController * textVC = [storyBoard instantiateViewControllerWithIdentifier:@"text"];
    [textVC setValue:_data[index.row] forKey:@"title"];
    
    return textVC;
}

//pop代理方法
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}

看一下效果图


                                          peek效果图                                                                                                  pop效果图


有的时候会想在peek的时候出现底部菜单,那么被弹出来的ViewController中就要遵守协议<UIPreviewActionItem>,实现如下方法
那么这个Demo楼主在TextViewController中实现如下方法
#pragma mark - UIPreviewActionItem
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    UIPreviewAction * act1 = [UIPreviewAction actionWithTitle:@"3D Touch" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
        //添加点击处理操作
        
    }];
    
    UIPreviewAction * act2 = [UIPreviewAction actionWithTitle:@"点个赞啊" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    UIPreviewAction * act3 = [UIPreviewAction actionWithTitle:@"小心啊" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
        
    }];
    
    return [NSArray arrayWithObjects:act1,act2,act3, nil];
}

效果图:

一个模仿iOS3D Touch效果的库,因为安卓本身不支持3D Touch,所以事件的触发是用长按点击来替代。项目地址:https://github.com/shalskar/PeekAndPop demo地址:https://github.com/shalskar/PeekAndPopDemo 效果图:使用说明:开始这个库托管在 Jitpack.io,所以在根 build.gradle文件中添加:allprojects {     repositories {        ...         maven { url "https://jitpack.io" }     } }然后在application的 build.gradle文件中添加如下依赖:dependencies {     compile 'com.github.shalskar:PeekAndPop:v0.1.1' }基本的使用很简单,只需一个activity实例,一个为 peek and pop准备的布局文件,一个或者多个在长按之后显示的 peek and pop视图。PeekAndPop peekAndPop = new PeekAndPop.Builder(this)                 .peekLayout(R.layout.peek_view)                 .longClickViews(view)                 .build();你可以调用PeekAndPop对象的getPeekView()来得到 peek view ,并使用 findViewById() 来得到 peek layout中的任意视图。View peekView = peekAndPop.getPeekView(); ImageView imageView = peekView.findViewById(R.id.image_view); TextView textView = peekView.findViewById(R.id.text_view);通常你可能还会想在列表中的某个item被点击时显示peek and pop ,为了让peek and pop正常工作,你需要添加这行代码:                .parentViewGroupToDisallowTouchEvents(viewGroup)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值