一個簡單的3DTouch、Peek和Pop手勢Demo,附github地址


 

參考文章:http://www.jianshu.com/p/74fe6cbc542b

下載鏈接:https://github.com/banchichen/3DTouch-PeekAndPopGestureDemo.git

前言:寫博客呢,一來可以記錄自己學習進步的點滴;二來可以邊寫博客邊復習下對應的知識;三來:還沒想到....。第一篇博客,排版、代碼等難免有瑕疵,見諒~

一、shortcutIems

1.6s和6s plus特有效果,對着應用圖標用力按會觸發。效果是這樣子的:每一個快捷按鈕對應一個shortcutItem,整個是一個數組,shortcutItems。

2.對應的代碼如下:也就三步,(1)配置shortcutItems;(2)判斷UIApplicationLaunchOptionsShortcutItemKey是否存在,在application:didFinishLaunchWithOptions里返回不同的值;(3)實現application:performActionForShortcutItem:completionHandler方法,處理shortcutItem的點擊事件。

 1 /*
 2  當程序啟動時
 3  1、判斷launchOptions字典內的UIApplicationLaunchOptionsShortcutItemKey是否為空
 4  2、當不為空時,application:didFinishLaunchWithOptions方法返回NO,否則返回YES
 5  3、在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 }

是的,總共就這么些代碼。

二、peek和pop手勢

1.也是6s和6s plus上才能觸發。效果是這樣子的:(peek手勢,預覽)

 是的,我就配了個Lable和背景View...比較粗糙...

 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,謝謝~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM