參考文章:http://www.jianshu.com/p/74fe6cbc542bgit
下載連接:https://github.com/banchichen/3DTouch-PeekAndPopGestureDemo.gitgithub
前言:寫博客呢,一來能夠記錄本身學習進步的點滴;二來能夠邊寫博客邊複習下對應的知識;三來:還沒想到....。第一篇博客,排版、代碼等不免有瑕疵,見諒~數組
1、shortcutIemsapp
1.6s和6s plus特有效果,對着應用圖標用力按會觸發。效果是這樣子的:每個快捷按鈕對應一個shortcutItem,整個是一個數組,shortcutItems。post
2.對應的代碼以下:也就三步,(1)配置shortcutItems;(2)判斷UIApplicationLaunchOptionsShortcutItemKey是否存在,在application:didFinishLaunchWithOptions裏返回不一樣的值;(3)實現application:performActionForShortcutItem:completionHandler方法,處理shortcutItem的點擊事件。學習
1 /* 2 當程序啓動時 3 一、判斷launchOptions字典內的UIApplicationLaunchOptionsShortcutItemKey是否爲空 4 二、當不爲空時,application:didFinishLaunchWithOptions方法返回NO,不然返回YES 5 三、在application:performActionForShortcutItem:completionHandler方法內處理點擊事件 6 */ 7 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 8 [self configShortCutItems]; 9 if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil) { 10 return YES; 11 } else { 12 return NO; 13 } 14 } 15 16 // 動態方式 建立shortcutItems 「已在info.plist裏配置好。這是代碼配置的示例。」 17 - (void)configShortCutItems { 18 NSMutableArray *shortcutItems = [NSMutableArray array]; 19 UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"測試1"]; 20 UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"測試2"]; 21 [shortcutItems addObject:item1]; 22 [shortcutItems addObject:item2]; 23 24 [[UIApplication sharedApplication] setShortcutItems:shortcutItems]; 25 } 26 27 // 處理shortcutItem 28 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { 29 switch (shortcutItem.type.integerValue) { 30 case 1: { // 測試1 31 [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoTestVc" object:self userInfo:@{@"type":@"1"}]; 32 } 33 case 2: { // 測試2 34 [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoTestVc" object:self userInfo:@{@"type":@"2"}]; 35 } break; 36 default: 37 break; 38 } 39 }
是的,總共就這麼些代碼。測試
2、peek和pop手勢spa
1.也是6s和6s plus上才能觸發。效果是這樣子的:(peek手勢,預覽)代理
是的,我就配了個Lable和背景View...比較粗糙...code
2.對應的代碼以下:步驟爲:
(1)讓控制器遵照協議 UIViewControllerPreviewingDelegate
(2)註冊 [self registerForPreviewingWithDelegate:self sourceView:self.view];
(3)實現代理方法
1 /** peek手勢 */ 2 - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { 3 UIViewController *childVC = [[UIViewController alloc] init]; 4 5 // 獲取用戶手勢點所在cell的下標。同時判斷手勢點是否超出tableView響應範圍。 6 if (![self getShouldShowRectAndIndexPathWithLocation:location]) return nil; 7 8 previewingContext.sourceRect = self.sourceRect; 9 10 // 加個白色背景 11 UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(20, 10, __kScreenWidth - 40, __kScreenHeight - 20 - 64 * 2)]; 12 bgView.backgroundColor = [UIColor whiteColor]; 13 bgView.layer.cornerRadius = 10; 14 bgView.clipsToBounds = YES; 15 [childVC.view addSubview:bgView]; 16 17 // 加個lable 18 UILabel *lable = [[UILabel alloc] initWithFrame:bgView.bounds]; 19 lable.textAlignment = NSTextAlignmentCenter; 20 lable.text = @"有種再按重一點..."; 21 [bgView addSubview:lable]; 22 23 return childVC; 24 } 25 26 /** pop手勢 */ 27 - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { 28 [self tableView:self.tableView didSelectRowAtIndexPath:self.indexPath]; 29 } 30 31 /** 獲取用戶手勢點所在cell的下標。同時判斷手勢點是否超出tableView響應範圍。*/ 32 - (BOOL)getShouldShowRectAndIndexPathWithLocation:(CGPoint)location { 33 NSInteger row = (location.y - 20)/50; 34 self.sourceRect = CGRectMake(0, row * 50 + 20, __kScreenWidth, 50); 35 self.indexPath = [NSIndexPath indexPathForItem:row inSection:0]; 36 // 若是row越界了,返回NO 不處理peek手勢 37 return row >= self.items.count ? NO : YES; 38 }
恩,總共就這些代碼。補充幾點說明:
(1)由於我這裏tableView只放了6條,因此這裏有判斷用戶的peek手勢觸摸點是否在tableView範圍內,在才返回預覽控制器,不然返回nil,不顯示預覽。
(2)sourceRect是peek觸發時的高亮區域。這個區域內的View會高亮顯示,其他的會模糊掉。你把sourceRect隨意改改試試~
(3)末尾再放一下Demo地址:https://github.com/banchichen/3DTouch-PeekAndPopGestureDemo.git 以爲好的能夠給個star,謝謝~