1 概述
上篇文章《iOS-3DTouch學(xué)習(xí)一:添加主屏快速操作》我們一起了解了在主屏幕的3D Touch快速操作,本篇我們一起學(xué)一下在應(yīng)用內(nèi)使用3D Touch,對內(nèi)容進(jìn)行展示自定義預(yù)覽和操作。
官方開發(fā)文檔
2 場景
- 使用一定力度按壓屏幕,觸發(fā)Peek操作。
- 在Peek狀態(tài)下,上劃喚出Peek快速操作(UIPreviewAction)。
- Peek狀態(tài)下,再次用更大力度按壓屏幕,轉(zhuǎn)場到預(yù)覽的控制器。
簡易OC版本Demo地址、官方swift版本地址
3 相關(guān)說明
-
UIViewControllerPreviewingDelegate:觸發(fā)3D Touch的類,需要遵循該協(xié)議,并要實現(xiàn)協(xié)議中的方法。 -
UIPreviewAction:Peek狀態(tài)下,上劃喚出快速操作選項實例。 -
previewActionItems:Peek狀態(tài)下,上劃喚出快速操作選項實例數(shù)組。在要被Pop出的控制器中覆寫get方法,返回一個UIPreviewAction數(shù)組。
4使用
4.1 注冊
在遵循UIViewControllerPreviewingDelegate協(xié)議的控制器中調(diào)用registerForPreviewingWithDelegate: sourceView:
遵循協(xié)議
// 遵循協(xié)議
@interface MainViewController ()<UIViewControllerPreviewingDelegate>
@end
注冊代理,并傳入響應(yīng)3D Touch的視圖
// Peek & Pop Demo code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(150, 200, 150, 40)];
// 開啟用戶交互
label.userInteractionEnabled = YES;
label.text = @"Peek & Pop";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
self.view.backgroundColor = [UIColor lightGrayColor];
// 注冊代理,并傳入響應(yīng)3D Touch的視圖
[self registerForPreviewingWithDelegate:self sourceView:label];
4.2 實現(xiàn)UIViewControllerPreviewingDelegate協(xié)議
可以通過設(shè)置previewingContext.sourceRect啟用其他UI元素的模糊,區(qū)域內(nèi)不被虛化,并在預(yù)覽時放大動畫。
如果不設(shè)置,將使用系統(tǒng)提供的合適區(qū)域。
當(dāng)系統(tǒng)檢測到3D Touch時,它會調(diào)用previewingContext:viewControllerForLocation代理方法,傳遞一個符合UIViewControllerPreviewing協(xié)議的previewingContext對象。使用此方法配置并返回一個視圖控制器以進(jìn)行預(yù)覽。
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
PopedViewController *popVC = [[PopedViewController alloc] init];
// 啟用其他UI元素的模糊,區(qū)域內(nèi)不被虛化,并在預(yù)覽時放大動畫。
// 在每次調(diào)用之前,此rect將設(shè)置為sourceView的邊界previewingContext:viewControllerForLocation:
// 如果不設(shè)置,將使用系統(tǒng)提供的合適區(qū)域。
previewingContext.sourceRect = CGRectMake(150, 200, 150, 40);
// 如果返回的是nil,則不會執(zhí)行預(yù)覽(Peek)操作
return popVC;
}
當(dāng)系統(tǒng)察覺到足夠的壓力可以觸發(fā)3D Touch 而Pop出ViewController時,會調(diào)用previewingContext:commitViewController:代理方法:
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
// 將配置好的控制器推入導(dǎo)航棧
[self.navigationController pushViewController:viewControllerToCommit animated:YES];
}
4.3 添加Peek狀態(tài)下,上劃時的快速操作
如果我們想在預(yù)覽的時候,想做一些操作,那我們可以在Peek狀態(tài)下,添加一些Action或者Group,以提供快速操作。要添加快速操作,需要我們在控制器中覆寫previewActionItems方法。
UIPreviewAction *firstAction = [UIPreviewAction actionWithTitle:@"選項一" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"選中了第一個action");
}];
UIPreviewAction *secondAction = [UIPreviewAction actionWithTitle:@"選項二" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"選中了第二個action");
}];
UIPreviewAction *thirdAction = [UIPreviewAction actionWithTitle:@"選項三" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"選中了第三個action");
}];
// 你可以直接返回三個選項
// 可以返回不同的分組,當(dāng)你點擊分組時,會再次彈出分組中的actions
// 當(dāng)然也可以同時返回
UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"group" style:UIPreviewActionStyleDefault actions:@[firstAction,secondAction]];
return @[group,thirdAction];
至此,簡單的3D Touch使用我們就學(xué)習(xí)完了,希望能夠幫助一些小伙伴。如果小伙伴覺得有幫助,記得給個小????哦。
來張效果圖
