3DTouch Peek和Pop功能

在我們的app中使用3D Touch功能,主要分為以下三個模塊:

  1. Home Screen Quick Actions
    點擊主屏幕app圖標呼出功能鍵,這部分功能在前一篇3D Touch簡單使用已經講了。

  2. peek and pop
    這個功能是點擊某一個Cell,此時Cell會顯示高亮狀態(下面有圖顯示),其余部分模糊處理(以上這個操作稱為Peek操作),在Peek操作下你還能進行兩個深度操作:繼續用手指按一下(這個操作就是Pop操作),或者向上拖動彈出來的這個視圖。

  3. 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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • MySQL支持的數據類型非常多,選擇正確的數據類型對于獲得高性能至關重要。不管存儲哪種類型的數據,下面幾個簡單原則...
    FX_SKY閱讀 495評論 0 1
  • 忙碌又迷茫,空虛又慌張!經歷了一天的焦灼等待,最終還是沒輪到我上場。最后一組講課比賽將以我為首在下周一進行。連續三...
    Lady_Lydia閱讀 126評論 0 0
  • /Applications/Xcode.app/Contents/Developer/Platforms/iPho...
    迷路的安然和無恙閱讀 4,921評論 0 1
  • 感恩今天跟蘭姐去超市買零食…聽說心情不好就去喂飽胃……零食就是好吃……想去KTV唱歌…她們幾個說明天…明天就明天吧...
    燦寶兒閱讀 178評論 0 0