一个简单的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 当 ...
随机推荐
- NFS性能优化
参考: http://www.techrepublic.com/blog/linux-and-open-source/tuning-nfs-for-better-performance/ 1.服务器端 ...
- [算法导论]二叉查找树的实现 @ Python
<算法导论>第三版的BST(二叉查找树)的实现: class Tree: def __init__(self): self.root = None # Definition for a b ...
- 每日英语:Mistrust Between U.S., Malaysia Strains Probe
Mistrust between U.S. and Malaysian air-accident investigators has hampered a multinational probe in ...
- C# inline-hook / api-hook
我查阅了一下相关C#方面的资料,却没有发现有提供过关于api-hook方面的资 料包括应用库由此本人编写一套inline-hook的库用于支持x64.x86上的基于在 clr的公共语言,如: c#.c ...
- 文件系统管理 之 Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍
一.df 命令:df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 举例: [root@localhost beinan] ...
- C++读取mysql中utf8mb4编码表数据乱码问题及UTF8转GBK编码
数据库编码为utf8,但是由于某些表的一些字段存储了emoji字符,表采用了utf8mb4编码,默认情况下在C++代码中读出的中文字段值都变成了乱码. 解决方法为,在进行数据库查询前,在C++中执行一 ...
- Swift:如何判断一个对象是否是某个类(或其子类)的实例
在OC中我们直接可以用如下方法即可 [obj isKindOfClass:[obj class]]; 在Swift中,并没有 .class 属性或者方法, 便可以用如下方法 class Person ...
- Entity Framework: Joining in memory data with DbSet
转载自:https://ilmatte.wordpress.com/2013/01/06/entity-framework-joining-in-memory-data-with-dbset/ The ...
- 将数据库表导入到solr索引
将数据库表导入到solr索引 编辑solrcofnig.xml添加处理器 <requestHandler name="/dataimport" class="org ...
- WorkbookDesigner mvc里面返回file
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...