前一篇文章介绍了给图片添加快捷方式,这篇主要介绍应用内,3D Touch的peek和pop功能的实现
-
当前的ViewController实现
UIViewControllerPreviewingDelegate
,实现代理方法- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
-
注册当前的viewController
/** 注册当前view */
[self registerForPreviewingWithDelegate:self sourceView:self.view];
代码如下:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *mainTable;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"首页";
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您的手机支持3dtouch" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
/** 注册当前view */
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"很遗憾您的手机不支持3dtouch" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
mainTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
mainTable.delegate = self;
mainTable.dataSource = self;
[self.view addSubview:mainTable];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = [NSString stringWithFormat:@"%ld--%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = cellId;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 15.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - previewing Delegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
/** 转换坐标 */
CGPoint p = [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)
{
return nil;
}
return detail;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 预览时滑动底部菜单添加,在要展示的ViewController中(本例为DemoViewController)实现 UIViewControllerPreviewingDelegate的协议
重写方法代理方法- (NSArray<id<UIPreviewActionItem>> *)previewActionItems;
代码如下:
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"title %@",self.titleString);
self.navigationItem.title = self.titleString;
}
底部预览界面选项
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *p1 = [UIPreviewAction actionWithTitle:@"选项1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"1111111");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"111111" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}];
UIPreviewAction *p2 = [UIPreviewAction actionWithTitle:@"选项2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"2222222222");
}];
UIPreviewAction *p3 = [UIPreviewAction actionWithTitle:@"选项3" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"3333333333");
}];
return @[p1,p2,p3];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
通过以上代码,就可实现3D touch peek和pop的效果
网友评论