3DTouch簡(jiǎn)介
3D Touch的觸控技術(shù),被蘋果稱為新一代多點(diǎn)觸控技術(shù)酣倾。其實(shí)舵揭,就是此前在Apple Watch上采用的Force Touch,
屏幕可感應(yīng)不同的感壓力度觸控躁锡。
3D Touch 午绳,蘋果iPhone 6s的新功能,看起來類似 PC 上的右鍵映之。有Peek Pop 兩種新手勢(shì)拦焚。
實(shí)現(xiàn)效果
其它不多講,直接上效果
效果一:
當(dāng)點(diǎn)擊AppIcon時(shí)彈出以下效果(起個(gè)專業(yè)點(diǎn)的名稱:AppIcon深按彈窗)
該效果實(shí)現(xiàn)分為兩步:
第一步:設(shè)置標(biāo)題
靜態(tài)設(shè)置:通過plist文件方式
動(dòng)態(tài)設(shè)置:通過代碼方式.
第二步:監(jiān)聽標(biāo)題點(diǎn)擊
第一步:設(shè)置標(biāo)題
靜態(tài)設(shè)置:通過plist文件配置,配置信息如果
動(dòng)態(tài)設(shè)置:通過代碼方式.
實(shí)現(xiàn)步驟:
在AppDelegate當(dāng)中書寫
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//設(shè)置圖標(biāo)長(zhǎng)按時(shí),彈出的樣式.
//iconWithType:圖標(biāo)的類型
UIApplicationShortcutIcon *icon0 =
[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
//創(chuàng)建第一個(gè)標(biāo)題
UIApplicationShortcutItem *item0 = [[UIApplicationShortcutItem alloc] initWithType:@"tpye0"
localizedTitle:@"標(biāo)題"
localizedSubtitle:@"我是子標(biāo)題"
icon:icon0
userInfo:@{@"info": @"我是要傳入的信息"}];
//創(chuàng)建第二個(gè)標(biāo)題
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"tpye0"
localizedTitle:@"標(biāo)題2"
localizedSubtitle:@"我是子標(biāo)題"
icon:icon0
userInfo:@{@"info": @"我是要傳入的信息"}];
//設(shè)置shortcutItems
application.shortcutItems = @[item0,item1];
return YES;
}
第二步:監(jiān)聽標(biāo)題點(diǎn)擊(在Appdelegate中監(jiān)聽)
//當(dāng)點(diǎn)擊AppIcon彈窗口,點(diǎn)擊標(biāo)題時(shí)調(diào)用
//shortcutItem點(diǎn)擊的是哪一個(gè)Item
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
{
//通過判斷標(biāo)題的類型,執(zhí)行相應(yīng)的操作
if ([shortcutItem.type isEqualToString:@"tpye0"]) {
NSLog(@"%@",shortcutItem.userInfo[@"info"]);
} else {
NSLog(@"asdf");
}
}
效果二:Peek和Pop
效果如下:
點(diǎn)擊一行Cell重按彈出以下效果
點(diǎn)擊peek出來的窗口繼續(xù)重按彈出以下效果
使用步驟:
1:給Cell注冊(cè)3DTouch
2:遵守協(xié)議<UIViewControllerPreviewingDelegate>
3:實(shí)現(xiàn)協(xié)議方法
第1步:給Cell注冊(cè)3DTouch
1.1 判斷是否支持3DTouch
1.2 注冊(cè)Cell支持3DTouch,并設(shè)置代理
實(shí)現(xiàn)代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
XqHeroCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
//判斷是否支持3DTouch
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//注冊(cè)Cell支持3DTouch,并設(shè)置代理
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
//取出當(dāng)前行模型
XqHeroItem *item = self.dataArray[indexPath.row];
cell.heroItem = item;
return cell;
}
第2步:遵守協(xié)議<UIViewControllerPreviewingDelegate>
@interface XqViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSArray *dataArray;
@end
第3步:實(shí)現(xiàn)協(xié)議方法
//當(dāng)中度按壓時(shí)調(diào)用該方法
//previewingContext:可以從該參數(shù)中獲取之前注冊(cè)的View.
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
//獲取sourceView
XqHeroCell *cell = (XqHeroCell *)[previewingContext sourceView];
//設(shè)置彈出預(yù)覽的位置(peek是從哪個(gè)位置彈出)
[previewingContext setSourceRect:cell.bounds];
//設(shè)置彈框的View.
XqDetailViewController *detailVC = [[XqDetailViewController alloc] init];
//設(shè)置彈出peek的高度(設(shè)置寬度是沒有效果的)
detailVC.preferredContentSize = CGSizeMake(0, 500);
//取出Cell的模型傳遞給詳情控制器.
detailVC.heroItem = cell.heroItem;
//設(shè)置標(biāo)題
detailVC.title = @“高俊";
//在這里想彈一個(gè)帶有導(dǎo)航條的控制器,控制器里面包裝一個(gè)導(dǎo)航條.直接返回導(dǎo)航控制器.那么就會(huì)peek出一個(gè)導(dǎo)航控制器.
return [[XqNavigationController alloc] initWithRootViewController:detailVC];
}
彈出效果如果下:
//彈框出現(xiàn)后,繼續(xù)重按時(shí)調(diào)用
//viewControllerToCommit:就是上面?zhèn)魅氲腦qDetailViewController的控制器.
//commitViewController默認(rèn)是UIViewController,因?yàn)閜eek時(shí)返回的控制器是一個(gè)導(dǎo)航控制器.那么在這里面自己手動(dòng)改成的導(dǎo)航控制器.
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UINavigationController *)viewControllerToCommit{
//獲取導(dǎo)航控制器的根控制器.因?yàn)楫?dāng)前已經(jīng)是一個(gè)導(dǎo)航控制器了,不能再繼續(xù)push一個(gè)導(dǎo)航控制器,所以要先獲取peek的導(dǎo)航控制器里面的根控制器.
//然后再拿當(dāng)前的控制器把獲取的控制器push進(jìn)去.
XqDetailViewController *detailVC = viewControllerToCommit.childViewControllers.lastObject;
[self.navigationController pushViewController:detailVC animated:YES];
//使用show和push是一樣的效果
//[self showViewController:viewControllerToCommit sender:self];
}
執(zhí)行效果如下:
相當(dāng)于點(diǎn)擊cell跳轉(zhuǎn)到下一個(gè)控制器.點(diǎn)擊Cell跳轉(zhuǎn)到下一個(gè)控制器代碼如果:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//創(chuàng)建控制器
XqDetailViewController *detailVC = [[XqDetailViewController alloc] init];
//獲取當(dāng)前選中的行模型
XqHeroItem *item = self.dataArray[indexPath.row];
//給詳情控制器模型賦值
detailVC.heroItem = item;
//跳轉(zhuǎn)到詳情控制器 [self.navigationController pushViewController:detailVC animated:YES];
}
效果三:彈窗Peek出現(xiàn)后,向上滑動(dòng),會(huì)出現(xiàn)類似ActionSheet的控件.
效果如下:
實(shí)現(xiàn)該效果必須得要是在peek出來的那個(gè)控制器當(dāng)中實(shí)現(xiàn)以下方法,
(我們這里peek出來的是一個(gè)導(dǎo)航控制器,所以必須得要在導(dǎo)航控制器中實(shí)現(xiàn)該方法)
//設(shè)置控制器在彈窗時(shí)候,下面輸出的數(shù)組
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems{
//彈出的第一個(gè)按鈕
UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@"action0" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action0 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action1 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action2 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"action3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"%s, line = %d, action2 = %@, previewViewController = %@", __FUNCTION__, __LINE__, action, previewViewController);
}];
//該按鈕可以是一個(gè)組,點(diǎn)擊該組時(shí),跳到組里面的按鈕.
UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"actionGroup" style:UIPreviewActionStyleSelected actions:@[action2, action3]];
//直接返回?cái)?shù)組.
return @[action0,action1,actionGroup];
}