国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 17:59:19
字體:
來源:轉載
供稿:網友

 

參考文章: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 // 處理shortcutItem28 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {29     switch (shortcutItem.type.integerValue) {30         case 1: { // 測試131             [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoTestVc" object:self userInfo:@{@"type":@"1"}];32         }33         case 2: { // 測試234             [[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     // 加個lable18     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,謝謝~

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 独山县| 富顺县| 河西区| 新建县| 新竹市| 景泰县| 涿鹿县| 文山县| 河南省| 酉阳| 西华县| 富平县| 顺义区| 太仓市| 泰来县| 青铜峡市| 手机| 定远县| 宣威市| 繁峙县| 马尔康县| 上犹县| 河北区| 临漳县| 即墨市| 汉阴县| 同江市| 通江县| 西贡区| 通榆县| 无锡市| 金山区| 徐水县| 北辰区| 威海市| 苗栗市| 汝南县| 崇左市| 广平县| 富蕴县| 长垣县|