写一个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. logback.xml配置

    一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文 ...

  2. excel 经验总结

    1.2007版excel表格中怎么将使用字母+数字下拉排序 比如:A201110300001怎么在excel表格中往下拉的时候变成A201110300002.A201110300003…… 方法: 因 ...

  3. Launcher2编译

    Android的源码包,压缩文件大概有3个G左右,要使用其中自带的一些源码需要很多技巧,否则会提示找不到一些库,大量的报错让人心神不定,不知所从. 我拿桌面代码举个例子吧. 桌面代码在源码包的pack ...

  4. IOS UIScrollView中 使用 touch 无法响应的问题

    添加一个 Category  然后在使用到 UIScrollView 的文件里面 导入这个头文件 就可以 // //  UIScrollView+UITouch.m //  alarm // //  ...

  5. STM32F407 外扩SRAM

    字节控制功能.支持高/低字节控制. 看看实现 IS62WV51216 的访问,需要对 FSMC进行哪些配置. 这里就做一个概括性的讲解.步骤如下: 1)使能 FSMC 时钟,并配置 FSMC 相关的  ...

  6. SqlServer中输出错误消息

    raiserror('所有参数都不能为空',16,1,@@error)

  7. DOM笔记(三):Element接口和HTMLElement接口

    一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...

  8. linux进程调度函数浅析(基于3.16-rc4)

    众所周知,进程调度使用schedule()函数来完成,下面我们从分析该函数开始,代码如下(kernel/sched/core.c): asmlinkage __visible void __sched ...

  9. ORM 是一种讨厌的反模式

    本文由码农网 – 孙腾浩原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! (“Too Long; Didn’t Read.”太长不想看,可以看这段摘要 )ORM是一种讨厌的反模式,违背 ...

  10. ubuntu 备份安装程序列表

    一般情况下,我们重装ubuntu的系统会做如下几个事情 1)修改默认的程序更新源 2)开始根据需求安装软件. 3)配置文件(如vim/tmux等) 对于步骤,只需要cp /etc//etc/apt/s ...