一个简单的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、判断launchOptions字典内的UIApplicationLaunchOptionsShortcutItemKey是否为空
2、当不为空时,application:didFinishLaunchWithOptions方法返回NO,否则返回YES
3、在application:performActionForShortcutItem:completionHandler方法内处理点击事件
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self configShortCutItems];
if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil) {
return YES;
} else {
return NO;
}
} // 动态方式 创建shortcutItems 「已在info.plist里配置好。这是代码配置的示例。」
- (void)configShortCutItems {
NSMutableArray *shortcutItems = [NSMutableArray array];
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"" localizedTitle:@"测试1"];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"" localizedTitle:@"测试2"];
[shortcutItems addObject:item1];
[shortcutItems addObject:item2]; [[UIApplication sharedApplication] setShortcutItems:shortcutItems];
} // 处理shortcutItem
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
switch (shortcutItem.type.integerValue) {
case : { // 测试1
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoTestVc" object:self userInfo:@{@"type":@""}];
}
case : { // 测试2
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoTestVc" object:self userInfo:@{@"type":@""}];
} break;
default:
break;
}
}
是的,总共就这么些代码。
二、peek和pop手势
1.也是6s和6s plus上才能触发。效果是这样子的:(peek手势,预览)

是的,我就配了个Lable和背景View...比较粗糙...
2.对应的代码如下:步骤为:
(1)让控制器遵守协议 UIViewControllerPreviewingDelegate
(2)注册 [self registerForPreviewingWithDelegate:self sourceView:self.view];
(3)实现代理方法
/** peek手势 */
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
UIViewController *childVC = [[UIViewController alloc] init]; // 获取用户手势点所在cell的下标。同时判断手势点是否超出tableView响应范围。
if (![self getShouldShowRectAndIndexPathWithLocation:location]) return nil; previewingContext.sourceRect = self.sourceRect; // 加个白色背景
UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(, , __kScreenWidth - , __kScreenHeight - - * )];
bgView.backgroundColor = [UIColor whiteColor];
bgView.layer.cornerRadius = ;
bgView.clipsToBounds = YES;
[childVC.view addSubview:bgView]; // 加个lable
UILabel *lable = [[UILabel alloc] initWithFrame:bgView.bounds];
lable.textAlignment = NSTextAlignmentCenter;
lable.text = @"有种再按重一点...";
[bgView addSubview:lable]; return childVC;
} /** pop手势 */
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self tableView:self.tableView didSelectRowAtIndexPath:self.indexPath];
} /** 获取用户手势点所在cell的下标。同时判断手势点是否超出tableView响应范围。*/
- (BOOL)getShouldShowRectAndIndexPathWithLocation:(CGPoint)location {
NSInteger row = (location.y - )/;
self.sourceRect = CGRectMake(, row * + , __kScreenWidth, );
self.indexPath = [NSIndexPath indexPathForItem:row inSection:];
// 如果row越界了,返回NO 不处理peek手势
return row >= self.items.count ? NO : YES;
}
恩,总共就这些代码。补充几点说明:
(1)因为我这里tableView只放了6条,所以这里有判断用户的peek手势触摸点是否在tableView范围内,在才返回预览控制器,否则返回nil,不显示预览。
(2)sourceRect是peek触发时的高亮区域。这个区域内的View会高亮显示,其余的会模糊掉。你把sourceRect随意改改试试~
(3)末尾再放一下Demo地址:https://github.com/banchichen/3DTouch-PeekAndPopGestureDemo.git 觉得好的可以给个star,谢谢~
一个简单的3DTouch、Peek和Pop手势Demo,附github地址的更多相关文章
- 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo
简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...
- 一、项目基础架构(附GitHub地址)——以ABP为基础架构的一个中等规模的OA开发日志
前言: 最近园子里ABP炒的火热.看了几篇对于ABP的介绍后,深感其设计精巧,实现优雅.个人感觉,ABP或ABP衍生品的架构设计,未来会成为中型Net项目的首选架构模式.如果您还不了解ABP是什么,有 ...
- 一个简单的scrapy爬虫抓取豆瓣刘亦菲的图片地址
一.第一步是创建一个scrapy项目 sh-3.2# scrapy startproject liuyifeiImage sh-3.2# chmod -R 777 liuyifeiImage/ 二.分 ...
- 神经进化学的简介和一个简单的CPPN(Compositional Pattern Producing Networks)DEMO
近期迷上神经进化(Neuroevolution)这个方向,感觉是Deep Learning之后的一个非常不错的研究领域. 该领域的一个主导就是仿照人的遗传机制来进化网络參数与结构.注意,连网络结构都能 ...
- 一个简单的代码计算行数demo编写
最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录 ...
- 一个简单的AMD模块加载器
一个简单的AMD模块加载器 参考 https://github.com/JsAaron/NodeJs-Demo/tree/master/require PS Aaron大大的比我的完整 PS 这不是一 ...
- 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)
这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...
- 手写一个简单版的SpringMVC
一 写在前面 这是自己实现一个简单的具有SpringMVC功能的小Demo,主要实现效果是; 自己定义的实现效果是通过浏览器地址传一个name参数,打印“my name is”+name参数.不使用S ...
- iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
[iOS]iOS9 3DTouch.ShortcutItem.Peek And Pop技术一览 3DTouch UITouch类里API的变化 iOS9中添加的属性 altitudeAngle 当 ...
随机推荐
- 私有云android客户端2.1.2最新版本(ownCloud简体中文优化版)
通过安装Ubuntu16.04+LAMP+ownCloud9.1+SSL建立私有云,下载ownCloud android客户端最新源码,针对国际语言简体中文化,修改部分代码,并进行补充.优化,编译生成 ...
- Form 表单中的Input元素回车时不保存表单
在Form表单中如果直接在Input元素里敲回车键,那么默认将提交表单,可以通过keydown事件取消默认此操作 $("form").live('keydown',function ...
- Linux 统计某个字符串出现的次数
要统计一个字符串出现的次数,这里现提供自己常用两种方法: 1. 使用vim统计 用vim打开目标文件,在命令模式下,输入 :%s/objStr//gn 即可 2. 使用grep: grep -o ob ...
- 使用jQuery页面加载函数启动定时任务
var timer; $(document).ready(function(){ if (timer == undefined) timer = window.setInterval(showTime ...
- lua中常量的实现及表的深拷贝实现
废话:好久没在这里写博客了...主要原因是我买了个域名hanxi.info并在github上搭建了个人博客... lua中默认是没有c中的const常量的,在csdn上找到了一个使用setmetata ...
- A simple visualization of energy function and energy gap in hopfield nets
- Generating a new ASP.NET session in the current HTTPContext
void regenerateId() { System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState ...
- Promising Linking
Future/Promise 执行逻辑 scala Future 有几个要点,第一个是 tryAwait 需要借助 CowndownLatch 实现,第二个是可以在 Promise 挂载回调函数 首先 ...
- WPF之DataGrid
1.WPF 4 DataGrid 控件(基本功能篇) 基本使用,绑定数据展示 2.WPF 4 DataGrid 控件(自定义样式篇) 定义行,列,头,单元格等样式 3.WPF 4 DataGrid 控 ...
- messages.exe病毒的清理
一.问题提出 20161021,10点05分,发现messages.exe突然出现在进程中,计算机突然重启,msconfig启动目录里多了一项c:\windows\system32\Drivers\m ...