写一个Demo来自定义一个长触屏事件,自定义长按手势。

实现步骤:

1.创建一个自定义手势类,命名为LongPressGestureRecognizer,在创建的时候继承UIGestureRecognizer

LongPressGestuRecognizer.h:

#import <UIKit/UIKit.h>

@interface LongPressGestureRecognizer : UIGestureRecognizer

@end

LongPressGestuRecognizer.m:


#import "LongPressGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#import <time.h> NSInteger timer1;
NSInteger timer2;
@implementation LongPressGestureRecognizer -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
NSDate *nowDate = [NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"ss"]; timer1 = [[dateformatter stringFromDate:nowDate] integerValue];
[dateformatter release];
NSLog(@"%d",timer1);
} -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
NSDate *nowDate = [NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"ss"]; [dateformatter release];
NSLog(@"%d",timer1); if ((timer2 -timer1) >= 2)
{
self.state = UIGestureRecognizerStateEnded;
} } @end

2.修改主ViewController

ViewController.h:

#import <UIKit/UIKit.h>

@interface DXWViewController : UIViewController<UIGestureRecognizerDelegate>

@end

ViewController.m:

#import "DXWViewController.h"

#import "LongPressGestureRecognizer.h"

@interface DXWViewController ()

@end

@implementation DXWViewController

- (void)viewDidLoad
{
[super viewDidLoad]; LongPressGestureRecognizer * longPress = [[LongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPress:)];
[self.view addGestureRecognizer:longPress];
} -(void)LongPress:(LongPressGestureRecognizer *)my
{
NSLog(@"OK");
} @end

3.ViewController中的触屏事件touchesBegan和自定义手势中的touchesBegan区别:

ViewController中的touchesBegan是针对整个View而言的,而自定义中的手势是要绑定到某个特定的view,只针对这个view才相应的手势事件



[IOS]自定义长触屏事件的更多相关文章

  1. cocos2d-x触屏事件(单点触屏)

    转自:http://blog.csdn.net/onerain88/article/details/7550009 一般经常用到的触屏的情况有两种:一种是Layer统一接收触屏消息,然后由程序根据需要 ...

  2. 移动端touch触屏滑动事件、滑动触屏事件监听!

    一.触摸事件 ontouchstart.ontouchmove.ontouchend.ontouchcancel 目前移动端浏览器均支持这4个触摸事件,包括IE.由于触屏也支持MouseEvent,因 ...

  3. 转:Android随笔之——使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)

    本文转载自CSDN的jzj1993,原文连接:http://blog.csdn.net/jzj1993/article/details/39158865 有时我们需要使用安卓实现在后台模拟系统按键,比 ...

  4. HTML5学习总结-09 拖放和手机触屏事件

    一 拖放 拖放(Drag 和 drop)是 HTML5 标准的组成部分.拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 课程参考 ht ...

  5. (转)js的左右滑动触屏事件

    原文:http://blog.sina.com.cn/s/blog_6a0a183f0100zsfk.html (2012-01-20 08:55:53) 转载▼ 标签: 移动设备 触屏事件 杂谈 分 ...

  6. js触屏事件

    js的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend.这三个事件最重要的属性是 pageX和 pageY,表示X,Y坐标. touchstart在触摸开始 ...

  7. Android:触屏事件

    Android触屏事件包含两种: 1)屏幕触屏事件:重写onTouchEvent(MotionEvent event): 2)控件触屏事件:给控件注册触屏事件,setOnTouchEventListe ...

  8. 从零开始学 Web 之 移动Web(二)JD移动端网页,移动触屏事件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  9. 【Cocos2dx 3.3 Lua】触屏事件

    cocos2dx 3.x触屏时间分为单点触摸和多点触摸:     单点触摸:(即只有注册的Layer才能接收触摸事件)      多点触摸点单用法(多个Layer获取屏幕事件):           ...

随机推荐

  1. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  2. ORACLE临时表总结[转]

    临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...

  3. 《Python核心编程》 第十章 错误和异常

    10–1. 引发异常. 以下的哪个因素会在程序执行时引发异常? 注意这里我们问的并不是异常的原因. a) 用户 b) 解释器 c) 程序 d) 以上所有 e) 只有 b) 和 c) f) 只有 a) ...

  4. C++容器学习

    以前自学C++的时候就没怎么看容器,一直以来也没怎么编过C++程序,现在想用C++写点东西,突感容器类型有些生疏,故做此笔记.(参考<C++ primer> 容器:容纳特定类型对象的集合. ...

  5. <Chapter 2>2-2-2-1.介绍JSPs,JSTL,和EL(Introducing JSPs, JSTL, and EL)

    现在,我们的时钟显示了UTC时区的时间.我们希望我们的应用可以让用户自定义时区,并且为将来的访问记住用户的偏好.为了做到这一点,我们使用Google帐户来识别哪个用户正在使用这个应用. 在我们深入了解 ...

  6. matlab中的字符串数组与函数调用

    1, matlab中的字符串就是1维字符数组,即如: a = 'dddssd'; b = 'lsde'; c = [a, b]; 当然也可以: c= strcat(a, b); 2, matlab中的 ...

  7. https://github.com/mlzboy/spider-impl.git

    Installation - pygit2 0.24.0 documentation Python 2.7, 3.2+ or PyPy 2.6+ (including the development ...

  8. MVC使用Google OAuth[OWIN]注意事項

    1.前提條件,申請一個client id,頁面:https://console.developers.google.com/ 2.添加連接域名,javascript那欄位為域名即可,另一欄需要加上具體 ...

  9. homework-04 单词方阵

    问题描述 本次作业的题目要求利用给定的一组单词生成一个矩阵,矩阵的每个位置由一个字母填充,单词表中的每一个单词可以匹配矩阵中一段连续的序列,这段序列可以是横向,纵向或者是45度斜角方向,单词可以由左向 ...

  10. 什么是APNs证书?

    转载自 http://dev.xiaomi.com/doc/p=2977/index.html 什么是APNs和APNs证书? APNs(全称为Apple Push Notification Serv ...