在我们的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操作效果图如下:
Pop操作则是直接push到弹出视图,效果图就不上了。
向上拖动Peek操作效果图如下: