在我們的app中使用3D Touch功能,主要分為以下三個模塊:
Home Screen Quick Actions
點擊主屏幕app圖標呼出功能鍵,這部分功能在前一篇3D Touch簡單使用已經講了。peek and pop
這個功能是點擊某一個Cell,此時Cell會顯示高亮狀態(下面有圖顯示),其余部分模糊處理(以上這個操作稱為Peek操作),在Peek操作下你還能進行兩個深度操作:繼續用手指按一下(這個操作就是Pop操作),或者向上拖動彈出來的這個視圖。Force Properties
這部分主要講力度值,在這里我用不到也不會。
接受協議
@interface HomePageViewController ()<UIViewControllerPreviewingDelegate>
@end
注冊方法
- (void)viewDidLoad {
[super viewDidLoad];
// 判斷當前設備是否支持3DTouch
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
Peek 操作:(用力點擊某一個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;
// 獲取當前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 操作:(用力繼續某一個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];
}
}
向上拖動彈出視圖的操作
#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觸發時的高亮區域。這個區域內的View會高亮顯示,其余的會模糊掉。
Peek操作效果圖如下:
6E240204DF994C438D048CE7D9A2718B.png
Pop操作則是直接push到彈出視圖,效果圖就不上了。
向上拖動Peek操作效果圖如下:
060AA8152A785E33A42BCB29590419FB.png