iOS 3D Touch使用<二、 Peek、Pop使用>

3D Touch 的第二个功能:预览和弹出视图


1、创建一个viewcontroller作为window的rootviewcontroller(xcode7 这个需要设置,否则会崩溃)

           

2、创建一个预览视图控制器peekViewController,展示peek的内容,同时实现

3、viewcontroller控制器签署协议

UIViewControllerPreviewingDelegate(该协议是iOS9新特性)


4、在viewdidload里面检测是否支持3D Touch,设置代理


//必须进行是否支持3D Touch,3D Touch可以被关闭,还有一些ios设备不支持

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

        

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

    }


5、在viewcontroller里实现协议方法

//peek

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)

{

    PeekViewController *vc = [[PeekViewController alloc]init];

    //设置预览视图的大小

    vc.preferredContentSize = CGSizeMake(0, 330);

    //设置点击位置出现的白色块的大小

    previewingContext.sourceRect = CGRectMake(20, location.y - 10, [UIScreen mainScreen].bounds.size.width - 40, 50);

    return vc;

}



//pop 方法

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)

{

//可以进行视图跳转等操作    

[self.Navgationviewcontroller pushViewController:viewControllerToCommit animated:NO completion:nil];

}




6、在peekviewcontroller里面实现

-(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法


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

{

    NSMutableArray *actionItemArray = [NSMutableArray array];

    

    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"自定义1" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        

        //实现自己的逻辑处理

        

    }];

    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"自定义2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        

    }];

    

    

    [actionItemArray  addObject:action1];

    [actionItemArray addObject:action2];

    return actionItemArray;

}







一个模仿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
发出的红包

打赏作者

moxi_wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值