前一篇文章介紹了給圖片添加快捷方式,這篇主要介紹應用內,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的效果