6s和6s plus之后特有效果需纳,對(duì)著應(yīng)用圖標(biāo)用力按會(huì)觸發(fā)3DTouch .
第一步 : 3DTouch 設(shè)備支持檢測(cè):
檢測(cè)當(dāng)前的設(shè)備是否支持3DTouch
// 在iOS9中有一個(gè)新的枚舉
typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
UIForceTouchCapabilityUnknown = 0, // 未知的支持屬性
UIForceTouchCapabilityUnavailable = 1, // 不支持
UIForceTouchCapabilityAvailable = 2 // 支持
};
一般我們都在每個(gè)ViewController的生命周期中這樣做:
定義一個(gè)是否設(shè)備支持的BOOL值屬性
@property (nonatomic , assign) BOOL support3DTouch;
在生命周期函數(shù)中檢測(cè)支持與否
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//檢測(cè)當(dāng)前是否支持3DTouch
self.support3DTouch = [self support3DTouch];
}
在生命周期外檢測(cè)支持與否(因?yàn)橛锌赡艹隽松芷诤瘮?shù)而發(fā)生了變化)
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0) {
self.support3DTouch = [self support3DTouch];
}
檢測(cè)是否支持3DTouch的方法
- (BOOL)support3DTouch
{
// 如果開啟了3D touch
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
return YES;
}
return NO;
}
}
第二步 : 配置快捷視圖列表
創(chuàng)建快捷視圖列表有兩種方法:
1,一種是編輯info.plist文件中的UIApplicationShortcutItems,
通過可視化的界面添加鍵值對(duì)直接配置info.plist
2盲赊,另一種是使用代碼在工程中加入items
在工程的 AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
[self.window makeKeyAndVisible];
// 代碼創(chuàng)建快捷視圖列表的方法芥永,
[self create3DTouchShotItems];
return YES;
}
- (void)create3DTouchShotItems {
//創(chuàng)建快捷item的icon UIApplicationShortcutItemIconFile
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon3"];
//創(chuàng)建快捷item的userinfo UIApplicationShortcutItemUserInfo
NSDictionary *info1 = @{@"url":@"url1"};
NSDictionary *info2 = @{@"url":@"url2"};
NSDictionary *info3 = @{@"url":@"url3"};
//創(chuàng)建ShortcutItem
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_1" localizedTitle:@"掃一掃" localizedSubtitle:@"" icon:icon1 userInfo:info1];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_2" localizedTitle:@"smile" localizedSubtitle:@"微笑面對(duì)生活" icon:icon2 userInfo:info2];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_3" localizedTitle:@"購物" localizedSubtitle:@"Shopping" icon:icon3 userInfo:info3];
NSArray *items = @[item1, item2, item3];
[UIApplication sharedApplication].shortcutItems = items;
}
第三步 : 給列表視圖中的cell注冊(cè) 3DTouch 事件
1,首先,在首頁當(dāng)前控制器里遵守UIViewControllerPreviewingDelegate協(xié)議
UIViewControllerPreviewingDelegate
2蓬痒,在注冊(cè)前先判斷是否設(shè)備支持(也就是第一步)
3,注冊(cè): [self registerForPreviewingWithDelegate:self sourceView:cell];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLTableViewCell"];
if (cell == nil) {
cell = [ZLTableViewCell cellWithTableView:tableView];
}
cell.dataFrame = self.dataSource[indexPath.row];
//給cell注冊(cè)代理漆羔,使其支持3DTouch手勢(shì)
if (self.support3DTouch) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
return cell;
}
第四步: 完成UIViewControllerPreviewingDelegate 協(xié)議回調(diào)梧奢,實(shí)現(xiàn)Peek Pop
在首頁當(dāng)前控制器里,
#pragma mark - 3DTouch UIViewControllerPreviewingDelegate
Peek 實(shí)現(xiàn)代碼:
// 此方法是輕按控件時(shí)狱掂,跳出peek的代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
//防止重復(fù)加入
if ([self.presentedViewController isKindOfClass:[ZLPeekViewController class]])
{
return nil;
}
else
{
ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
ZLCellData * cellData = cell.dataFrame.cellData;
ZLPeekViewController *peekViewController = [[ZLPeekViewController alloc] init];
peekViewController.cellData = cellData;
peekViewController.delegate = self;
return peekViewController;
}
}
Pop 代碼
//此方法是重按peek時(shí),跳入pop的代理方法
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit {
ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
ZLCellData * cellData = cell.dataFrame.cellData;
ZLPopViewController *popViewController = [[ZLPopViewController alloc] init];
popViewController.cellData = cellData;
// 以prentViewController的形式展現(xiàn)
[self showViewController:popViewController sender:self];
// 以push的形勢(shì)展現(xiàn)
// [self.navigationController pushViewController:popViewController animated:YES];
}
第五步 : 在Peek狀態(tài)下向上滑動(dòng)出現(xiàn)的按鈕配置方法
在 ZLPeekViewController.m 里, 實(shí)現(xiàn) - (NSArray> *)previewActionItems 回調(diào)方法
#pragma mark - Preview Actions
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
// 生成UIPreviewAction
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"事件 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 1 selected");
[self.delegate pushToPopViewControllerWithCellData:self.cellData];
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"事件 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 2 selected");
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"事件 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 3 selected");
}];
UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"按鈕 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 1 selected");
}];
UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"按鈕 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 2 selected");
}];
UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"按鈕 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 3 selected");
}];
NSArray *actions = @[action1, action2, action3];
NSArray *taps = @[tap1, tap2, tap3];
UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"一組事件" style:UIPreviewActionStyleDefault actions:actions];
UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"一組按鈕" style:UIPreviewActionStyleDefault actions:taps];
NSArray *group = @[group1,group2];
//當(dāng)然你也可以返回三個(gè)單獨(dú)的action對(duì)象的數(shù)組亲轨,而不是group趋惨,具體效果,可以自己試一下
return group;
}
現(xiàn)在可以測(cè)試嘍, 看下效果吧, 如果需要demo可以去我的 GitHub 上下載~