目标

识别当前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的更多相关文章

  1. 已经重写,源码和文章请跳转http://www.cnblogs.com/ymnets/p/5621706.html

    文章由于写得比较仓促 已经重写,源码和文章请跳转 http://www.cnblogs.com/ymnets/p/5621706.html 系列目录 前言: 导入导出实在多例子,很多成熟的组建都分装了 ...

  2. Java程序性能优化Tip

    本博客是阅读<java time and space performance tips>这本小书后整理的读书笔记性质博客,增加了几个测试代码,代码可以在此下载:java时空间性能优化测试代 ...

  3. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  4. Entity Framework技巧系列之六 - Tip 20 – 25

    提示20. 怎样处理固定长度的主键 这是正在进行中的Entity Framework提示系列的第20篇. 固定长度字段填充: 如果你的数据库中有一个固定长度的列,例如像NCHAR(10)类型的列,当你 ...

  5. ntity Framework技巧系列之四 - Tip 13 – 15

    提示13. 附加一个实体的简单方式 问题: 在早先的一些提示中,我们讨论了使用Attach来加载一个处于未改变(unchanged)状态的东西到ObjectContext从而避免进行查询的开销. 如果 ...

  6. Url Rewrite 再说Url 重写

    前几天看到园子里一篇关于 Url 重写的文章<获取ISAPI_Rewrite重写后的URL>, URL-Rewrite 这项技术早已不是一项新技术了,这个话题也已经被很多人讨论过多次.搜索 ...

  7. Url Rewrite 重写

    前几天看到园子里一篇关于 Url 重写的文章<获取ISAPI_Rewrite重写后的URL>, URL-Rewrite 这项技术早已不是一项新技术了,这个话题也已经被很多人讨论过多次.搜索 ...

  8. 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章讲述可伸 ...

  9. 自定义alert弹框,title不显示域名(重写alert)

    问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...

随机推荐

  1. Java基础知识【下】( 转载)

    http://blog.csdn.net/silentbalanceyh/article/details/4608360 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...

  2. NSDateFormatter 时间格式转换

    NSString *strDate = @“Wed Apr ::”; NSDateFormatter *dateFomatter =[[NSDateFormatter alloc] init]; [d ...

  3. 2、项目标准的制定 - PMO项目管理办公室

    PMO项目管理办公室应该是针对公司级别的项目内容制定项目标准.这个由公司的项目经理们进行集中讨论,然后将项目标准进行文档化,标准化,从而为公司的项目进行指导性的功能,为公司的项目进行服务.下面对项目标 ...

  4. hbase开发实例

    1.put/checkAndPut package com.testdata; import java.io.IOException; import org.apache.hadoop.conf.Co ...

  5. Mac下QT错误,Xcode配置解决办法

    出现错误: Xcode not set up properly. You may need to confirm the license agreement by running / 解决办法如下: ...

  6. 实例:对2个Makefile的备注

    实例1:Makefile编译链接简单.c函数 example.c Makefile exe: example.c gcc example.c -o exe clean: rm exe 执行效果: 实例 ...

  7. [html/css]清除浮动的相关技巧

    以前只了解得很浅显,转载了一篇不错的文,学习参考 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的代码,在各种浏览器中显示效果也有可 ...

  8. 初识SpringMvc

    初识SpringMvc springMvc简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 s ...

  9. CMakeList.txt/Clion中添加头文件和库

    cmake_minimum_required(VERSION 3.6) project(capi_lua) include_directories(/usr/include) find_library ...

  10. phpmyadmin导入数据库大小限制修改

    phpmyadmin默认导入数据库文件大小为2M,但一般网站的数据库导出的文件都会超出这个限制,要导入超过2M的数据库文件就需要手动修改php.ini配置文件! 在php.ini文件中修改: uplo ...