美文网首页
APP内 3Dtouch POPVC功能实现

APP内 3Dtouch POPVC功能实现

作者: Charming_Zhang | 来源:发表于2017-02-03 17:20 被阅读26次

iOS9 3D Touch 使用第二篇

字数712阅读908评论2喜欢3

前一篇文章介绍了给图片添加快捷方式,这篇主要介绍应用内,3D Touch的peek和pop功能的实现

当前的ViewController实现UIViewControllerPreviewingDelegate,实现代理方法- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location

注册当前的viewController

/** 注册当前view */[selfregisterForPreviewingWithDelegate:selfsourceView:self.view];

代码如下:

#import"ViewController.h"#import"DetailViewController.h"@interfaceViewController(){UITableView*mainTable;}@end@implementationViewController- (void)viewDidLoad {    [superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.self.view.backgroundColor= [UIColorwhiteColor];self.title=@"首页";if(self.traitCollection.forceTouchCapability==UIForceTouchCapabilityAvailable)    {UIAlertView*alert = [[UIAlertViewalloc] initWithTitle:@"您的手机支持3dtouch"message:nildelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];        [alert show];/** 注册当前view */[selfregisterForPreviewingWithDelegate:selfsourceView:self.view];    }else{UIAlertView*alert = [[UIAlertViewalloc] initWithTitle:@"很遗憾您的手机不支持3dtouch"message:nildelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];        [alert show];    }    mainTable = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];    mainTable.delegate=self;    mainTable.dataSource=self;    [self.viewaddSubview:mainTable];}- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return1;}- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{return10;}- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{NSString*cellId = [NSStringstringWithFormat:@"%ld--%ld",(long)indexPath.section,(long)indexPath.row];UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellId];if(!cell)    {        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];    }    cell.textLabel.text= cellId;returncell;}- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{return45.0f;}- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{return15.0f;}- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{return0.1f;}- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark -  previewing Delegate- (UIViewController*)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location{/** 转换坐标 */CGPointp = [mainTable convertPoint:location fromView:self.view];/** 通过坐标活的当前cell indexPath */NSIndexPath*indexPath = [mainTable indexPathForRowAtPoint:p];/** 获得当前cell */UITableViewCell*cell = [mainTable cellForRowAtIndexPath:indexPath];    DetailViewController *detail = [[DetailViewController alloc] init];//    detail.preferredContentSize = CGSizeMake(0, 120);detail.view.frame=self.view.frame;    detail.titleString= cell.textLabel.text;if(indexPath.row>6)    {returnnil;    }returndetail;}- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController*)viewControllerToCommit{    [selfshowViewController:viewControllerToCommit sender:self];}- (void)didReceiveMemoryWarning {    [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end

预览时滑动底部菜单添加,在要展示的ViewController中(本例为DemoViewController)实现 UIViewControllerPreviewingDelegate的协议

重写方法代理方法- (NSArray> *)previewActionItems;

代码如下:

@interfaceDetailViewController()@end@implementationDetailViewController- (void)viewDidLoad {    [superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor= [UIColorlightGrayColor];}- (void)viewWillAppear:(BOOL)animated{    [superviewWillAppear:animated];NSLog(@"title  %@",self.titleString);self.navigationItem.title=self.titleString;}底部预览界面选项-(NSArray> *)previewActionItems{UIPreviewAction*p1 = [UIPreviewActionactionWithTitle:@"选项1"style:UIPreviewActionStyleDefaulthandler:^(UIPreviewAction* _Nonnull action,UIViewController* _Nonnull previewViewController) {NSLog(@"1111111");UIAlertView*alert = [[UIAlertViewalloc] initWithTitle:@""message:@"111111"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];        [alert show];    }];UIPreviewAction*p2 = [UIPreviewActionactionWithTitle:@"选项2"style:UIPreviewActionStyleSelectedhandler:^(UIPreviewAction* _Nonnull action,UIViewController* _Nonnull previewViewController) {NSLog(@"2222222222");    }];UIPreviewAction*p3 = [UIPreviewActionactionWithTitle:@"选项3"style:UIPreviewActionStyleDestructivehandler:^(UIPreviewAction* _Nonnull action,UIViewController* _Nonnull previewViewController) {NSLog(@"3333333333");    }];return@[p1,p2,p3];}- (void)didReceiveMemoryWarning {    [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}

通过以上代码,就可实现3D touch peek和pop的效果

相关文章

  • APP内 3Dtouch POPVC功能实现

    iOS9 3D Touch 使用第二篇 字数712阅读908评论2喜欢3 前一篇文章介绍了给图片添加快捷方式,这篇...

  • Android App Shortcuts的使用

    Android7.1(API 25)以后支持类似苹果的3dTouch功能,叫做App Shortcuts.在App...

  • 3D-Touch

    现在一些主流的APP都包含3Dtouch的功能,我们当然也不能落下,下面向大家推荐一个3Dtouch的De...

  • iOS 给App添加3DTouch功能

    给App添加3DTouch的多个Item 给某个页面添加3DTouch 3DTouch添加页面 预览页面

  • iOS开发之3DTouch详解

    自苹果在2015年发布3dtouch功能以来, iPhone 6s之后的机型全都匹配了 3dtouch功能。此功能...

  • IOS中3DTouch在tableView中的使用

    一 使用场景 3DTouch在IOS应用中,主要有2种功能:1 主屏幕上Icon快捷功能; 2 App内页面中的使...

  • 怎么给App的某个功能添加桌面快捷方式?

    iOS中给App添加快捷方式的几种方案: 3DTouch,长按App唤起3DTouch菜单,这个同时也可以当做小组...

  • 给App的某个功能添加桌面快捷方式

    iOS中给App添加快捷方式的几种方案: 3DTouch,长按App唤起3DTouch菜单,这个同时也可以当做小组...

  • 3D Touch

    去年6s上市之后推出了3DTouch功能,现在很多app都运用了次功能,我对这个功能一直很感兴趣,可是之前一直没钱...

  • Android 应用升级功能实现

    本文记录实现app应用内升级功能中遇到的问题 常见的app应用内升级常见步骤为:1,调用后台接口,比对当前版本号查...

网友评论

      本文标题:APP内 3Dtouch POPVC功能实现

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