[Tip]重写PanGestureRecognizer
目标
识别当前pan方向是指定方向
需要API
重写
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
获取所在view上的滑动速率- (CGPoint)velocityInView:(nullable UIView *)view;
通过速率排序当前pan方向,获取方向
代码:
.h
////仿照SSWDirectionalPanGestureRecognizer,仅作为学习项目使用。
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, HJPanDirection) {
HJPanDirectionRight,
HJPanDirectionDown,
HJPanDirectionLeft,
HJPanDirectionUp
};
@interface HJDirectionPanGestureRecognizer : UIPanGestureRecognizer
@property (nonatomic, assign) HJPanDirection direction;
@end
.m
#import "HJDirectionPanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface HJDirectionPanGestureRecognizer ()
@property (nonatomic, assign) BOOL dragging;
@end
@implementation HJDirectionPanGestureRecognizer
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) {
return;
}
CGPoint velocity = [self velocityInView:self.view];
if (!self.dragging && !CGPointEqualToPoint(velocity, CGPointZero)) {
NSDictionary * velocities = @{@(HJPanDirectionRight):@(velocity.x),
@(HJPanDirectionDown) :@(velocity.y),
@(HJPanDirectionLeft) :@(-velocity.x),
@(HJPanDirectionUp) :@(-velocity.y)};
NSArray * keysSorted = [velocities keysSortedByValueUsingSelector:@selector(compare:)];
if ([[keysSorted lastObject] integerValue] != self.direction) {
self.state = UIGestureRecognizerStateFailed;
}
self.dragging = YES;
}
}
////UIGestureRecognizerStateEnded after automatically called.
- (void)reset {
[super reset];
self.dragging = NO;//reset
}
@end
[Tip]重写PanGestureRecognizer的更多相关文章
- 已经重写,源码和文章请跳转http://www.cnblogs.com/ymnets/p/5621706.html
文章由于写得比较仓促 已经重写,源码和文章请跳转 http://www.cnblogs.com/ymnets/p/5621706.html 系列目录 前言: 导入导出实在多例子,很多成熟的组建都分装了 ...
- Java程序性能优化Tip
本博客是阅读<java time and space performance tips>这本小书后整理的读书笔记性质博客,增加了几个测试代码,代码可以在此下载:java时空间性能优化测试代 ...
- 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件
Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...
- Entity Framework技巧系列之六 - Tip 20 – 25
提示20. 怎样处理固定长度的主键 这是正在进行中的Entity Framework提示系列的第20篇. 固定长度字段填充: 如果你的数据库中有一个固定长度的列,例如像NCHAR(10)类型的列,当你 ...
- ntity Framework技巧系列之四 - Tip 13 – 15
提示13. 附加一个实体的简单方式 问题: 在早先的一些提示中,我们讨论了使用Attach来加载一个处于未改变(unchanged)状态的东西到ObjectContext从而避免进行查询的开销. 如果 ...
- Url Rewrite 再说Url 重写
前几天看到园子里一篇关于 Url 重写的文章<获取ISAPI_Rewrite重写后的URL>, URL-Rewrite 这项技术早已不是一项新技术了,这个话题也已经被很多人讨论过多次.搜索 ...
- Url Rewrite 重写
前几天看到园子里一篇关于 Url 重写的文章<获取ISAPI_Rewrite重写后的URL>, URL-Rewrite 这项技术早已不是一项新技术了,这个话题也已经被很多人讨论过多次.搜索 ...
- Web Scalability for Startup Engineers Tip&Techniques for Scaling You Web Application --读书笔记
Web Scalability for Startup Engineers Tip&Techniques for Scaling You Web Application 第1章和第2章讲述可伸 ...
- 自定义alert弹框,title不显示域名(重写alert)
问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...
随机推荐
- WEB基础原理——理论复习
基本WEB原理 1. Internet同Web的关系 1.1互联网 全世界最大的局域网. 来源美国国防部的项目用于数据共享 没有TCP/IP之前最开始只能1000台电脑通信(军用协议) 1.2 万维网 ...
- arcgis 许可异常的解决
异常现象: arcgis 许可服务管理器中无法重新读取许可,许可服务启动后立即停止. 解决方法: 1.卸载license:安装新的license!重新破解,替换license文件夹BI ...
- Critical: Update Your Windows Secure Channel (cve-2014-6321,MS14-066)
前言:风雨欲来山满楼,下半年开始各种凶猛的漏洞层出不穷,天下已经不太平,互联网已经进入一个新的台阶 0x01 cve-2014-6321 11月的补丁月,微软请windows的用户吃了顿大餐,发布了1 ...
- React Native之坑总结(持续更新)
React Native之坑总结(持续更新) Genymotion安装与启动 之前我用的是蓝叠(BlueStack)模拟器,跑RN程序也遇到了一些问题,都通过搜索引擎解决了,不过没有记录. 但是Blu ...
- SSH面试题(struts2+Spring+hibernate)
struts2 + Spring +hibernate Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory ...
- nodejs pm2部署配置
pm2是一个进程管理工具,可以用它来管理你的node进程,并查看node进程的状态,当然也支持性能监控,进程守护,负载均衡等功能. 1.pm2安装使用需要全局安装 npm install -g pm ...
- 运行Maven工程总是报错:No goals have been specified for this build
Error info: No goals have been specified for this build. You must specify a valid lifecycle phase or ...
- protobuf学习(2)-相关学习资料
protobuf官方git地址 protobuf官方英文文档 (你懂的需要FQ) protobuf中文翻译文档 protobuf概述 (官方翻译 推荐阅读) protobuf入门 ...
- 30天的php学习之路——第一天开端
因项目紧急只留给了我4周时间学习php.谨以此文记录我的php学习历程,仅供参考,若有错误请指正. 之前对php有过一些了解,一些语法和习惯和C#差距有些大,所以第一天准备对php中的符号有个全面的了 ...
- Usual tiny skills & solutions
Ubuntu and Win10 - double OS 2016-02-21 Yesterday I helped my friend install Ubuntu (14.04 LTS) on h ...