APP内 3Dtouch POPVC功能实现

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功能实现)