在我們的app中使用3D Touch功能,主要分為以下三個(gè)模塊:
Home Screen Quick Actions
點(diǎn)擊主屏幕app圖標(biāo)呼出功能鍵减噪,這部分功能在前一篇3D Touch簡單使用已經(jīng)講了。peek and pop
這個(gè)功能是點(diǎn)擊某一個(gè)Cell,此時(shí)Cell會(huì)顯示高亮狀態(tài)(下面有圖顯示),其余部分模糊處理(以上這個(gè)操作稱為Peek操作)送浊,在Peek操作下你還能進(jìn)行兩個(gè)深度操作:繼續(xù)用手指按一下(這個(gè)操作就是Pop操作),或者向上拖動(dòng)彈出來的這個(gè)視圖丘跌。Force Properties
這部分主要講力度值袭景,在這里我用不到也不會(huì)。
接受協(xié)議
@interface HomePageViewController ()<UIViewControllerPreviewingDelegate>
@end
注冊方法
- (void)viewDidLoad {
[super viewDidLoad];
// 判斷當(dāng)前設(shè)備是否支持3DTouch
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
Peek 操作:(用力點(diǎn)擊某一個(gè)Cell的效果)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
UIViewController *childVC = [[UIViewController alloc] init];
CGRect rect = CGRectMake(20, 20, SCREEN_WIDTH - 40, SCREEN_HEIGHT - 40 - 64*2);
previewingContext.sourceRect = rect;
// 獲取當(dāng)前indexPath
self.touchIndexPath = [self.apartTV indexPathForRowAtPoint:location];
switch (self.touchIndexPath.row) {
case 0:{
ChartLineViewController *chartVC = [[ChartLineViewController alloc] init];
chartVC.isCity = YES;
chartVC.title = [self.viewModel.dataSource objectForKey:@"TITLE"];
chartVC.city_name = [self.viewModel.dataSource objectForKey:@"TITLE"];
childVC = (ChartLineViewController *)chartVC;
break;
}
case 1:{
DeviceViewController *deviceVC = [[DeviceViewController alloc] init];
childVC = (DeviceViewController *)deviceVC;
break;
}
case 2:{
ChartLineViewController *chartVC = [[ChartLineViewController alloc] init];
chartVC.title = [[[self.viewModel.dataSource objectForKey:@"SENSOR"] objectAtIndex:0] objectForKey:@"ps"];
chartVC.isFirstSensor = true;
chartVC.linesArray = [NSMutableArray arrayWithArray:[self deleteInvalidData]];
childVC = (ChartLineViewController *)chartVC;
break;
}
default:
break;
}
return childVC;
}
Pop 操作:(用力繼續(xù)某一個(gè)Cell之后彈出視圖碍岔,再次Touch的效果)
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
self.navigationItem.backBarButtonItem = BACK_BARITEM;
if (self.touchIndexPath.row == 0) {
ChartLineViewController *chartVC = [[ChartLineViewController alloc] init];
chartVC.isCity = YES;
chartVC.title = [self.viewModel.dataSource objectForKey:@"TITLE"];
chartVC.city_name = [self.viewModel.dataSource objectForKey:@"TITLE"];
chartVC.refreshCityBlock = ^(NSString *title){
self.isChangeCity = true;
self.buttonTitle = title;
};
[self.navigationController pushViewController:chartVC animated:YES];
}
else if (self.touchIndexPath.row == 1){
DeviceViewController *deviceVC = [[DeviceViewController alloc] init];
[self.navigationController pushViewController:deviceVC animated:YES];
}
else{
ChartLineViewController *chartVC = [[ChartLineViewController alloc] init];
chartVC.title = [[[self.viewModel.dataSource objectForKey:@"SENSOR"] objectAtIndex:0] objectForKey:@"ps"];
chartVC.isFirstSensor = true;
chartVC.linesArray = [NSMutableArray arrayWithArray:[self deleteInvalidData]];
[self.navigationController pushViewController:chartVC animated:YES];
}
}
向上拖動(dòng)彈出視圖的操作
#pragma mark - 3DTouch Sliding Action
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
UIPreviewAction *itemAdd = [UIPreviewAction actionWithTitle:@"添加" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
// 添加操作
}];
UIPreviewAction *itemDelete = [UIPreviewAction actionWithTitle:@"刪除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
// 刪除操作
}];
return @[itemAdd,itemDelete];
}
sourceRect是peek觸發(fā)時(shí)的高亮區(qū)域浴讯。這個(gè)區(qū)域內(nèi)的View會(huì)高亮顯示朵夏,其余的會(huì)模糊掉蔼啦。
Peek操作效果圖如下:
6E240204DF994C438D048CE7D9A2718B.png
Pop操作則是直接push到彈出視圖,效果圖就不上了仰猖。
向上拖動(dòng)Peek操作效果圖如下:
060AA8152A785E33A42BCB29590419FB.png