iOS自定义手势识别器测量长按的长度
在我的应用程序中,我想创建一个自定义手势识别器,它可以识别长按然后轻扫的动作。 我需要测量长按的长度是否超过1秒。如果是,则调用函数并等待滑动操作开始 我的问题是,我现在知道媒体有多长的唯一方法是从TouchsMoved中提取TouchsStart的时间戳。然而,我想知道touchesMoved调用之前经过的时间 在调用touchesMoved之前,有没有办法知道轻触的长度iOS自定义手势识别器测量长按的长度,ios,uigesturerecognizer,Ios,Uigesturerecognizer,在我的应用程序中,我想创建一个自定义手势识别器,它可以识别长按然后轻扫的动作。 我需要测量长按的长度是否超过1秒。如果是,则调用函数并等待滑动操作开始 我的问题是,我现在知道媒体有多长的唯一方法是从TouchsMoved中提取TouchsStart的时间戳。然而,我想知道touchesMoved调用之前经过的时间 在调用touchesMoved之前,有没有办法知道轻触的长度 提前谢谢 你可以使用may代码,它可以使用,但也许你应该处理一些细节 在.h文件中,应添加以下ivar: Test
提前谢谢 你可以使用may代码,它可以使用,但也许你应该处理一些细节 在.h文件中,应添加以下ivar:
TestView *aView ;//the view which you press
NSThread *timerThread;
NSTimer *touchTimer;
NSDate *touchStartTime;
CGPoint touchStartPoint;
CGPoint lastTouchPoint;
在.m文件中,应添加以下方法:
- (void)doSomething
{
NSLog(@"Long press!!!");
}
- (void)startTimerThead{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(checkTouchTime:)
userInfo:nil repeats:YES];
[runLoop run];
[pool release];
}
- (void)stopTouchTimer{
if (touchTimer != nil) {
[touchTimer invalidate];
touchTimer = nil;
}
if (timerThread != nil) {
[timerThread cancel];
[timerThread release];
timerThread = nil;
}
}
#define DELETE_ACTIVING_TIME 1.0
- (void)checkTouchTime:(NSTimer*)timer{
NSDate *nowDate = [NSDate date];
NSTimeInterval didTouchTime = [nowDate timeIntervalSinceDate:touchStartTime];
if (didTouchTime > DELETE_ACTIVING_TIME){
[self stopTouchTimer];
[self doSomething];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
if ([touchView isKindOfClass:[TestView class]]) {
touchStartTime = [[NSDate date] retain];
if (nil == timerThread) {
timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThead) object:nil];
[timerThread start];
}
}
touchStartPoint = [touch locationInView:self.view];
lastTouchPoint = touchStartPoint;
}
#define TOUCH_MOVE_EFFECT_DIST 10.0f
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint movedPoint = [touch locationInView:self.view];
CGPoint deltaVector = CGPointMake(movedPoint.x - touchStartPoint.x, movedPoint.y - touchStartPoint.y);
if (fabsf(deltaVector.x) > TOUCH_MOVE_EFFECT_DIST
|| fabsf(deltaVector.y) > TOUCH_MOVE_EFFECT_DIST)
{
[self stopTouchTimer];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
CGPoint movedPoint = [touch locationInView:self.view];
CGPoint deltaVector = CGPointMake(movedPoint.x - touchStartPoint.x, movedPoint.y - touchStartPoint.y);
if ([touchView isKindOfClass:[TestView class]]) {
if (fabsf(deltaVector.x) < TOUCH_MOVE_EFFECT_DIST
&& fabsf(deltaVector.y) < TOUCH_MOVE_EFFECT_DIST) {
[self stopTouchTimer];
}
}
}
-(无效)剂量测量
{
NSLog(@“长按!!!”);
}
-(无效)startTimerThead{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
NSRunLoop*runLoop=[NSRunLoop currentlunloop];
touchTimer=[NSTimer scheduledTimerWithTimeInterval:0.2
目标:自我
选择器:@selector(checkTouchTime:)
userInfo:nil repeats:YES];
[运行循环运行];
[池释放];
}
-(无效)stopTouchTimer{
如果(触摸计时器!=nil){
[触摸计时器失效];
触摸计时器=零;
}
如果(timerThread!=nil){
[时间读取取消];
[定时读取释放];
timerThread=零;
}
}
#定义删除活动时间1.0
-(无效)checkTouchTime:(NSTimer*)计时器{
NSDate*nowDate=[NSDate日期];
NSTimeInterval didTouchTime=[nowDate TimeIntervalsIncestate:touchStartTime];
如果(didTouchTime>删除活动时间){
[自动停止触摸计时器];
[自我剂量测定];
}
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
UIView*touchView=[触摸视图];
if([touchView IsKindof类:[TestView类]]){
touchStartTime=[[NSDate date]retain];
if(nil==timerThread){
timerThread=[[NSThread alloc]initWithTarget:自选择器:@selector(startTimerThead)对象:nil];
[时间读取开始];
}
}
touchStartPoint=[触摸位置视图:self.view];
lastTouchPoint=触摸起点;
}
#定义触摸\移动\效果\距离10.0f
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint movedPoint=[触摸位置视图:self.view];
CGPoint deltaVector=CGPointMake(movedPoint.x-touchStartPoint.x,movedPoint.y-touchStartPoint.y);
如果(fabsf(deltaVector.x)>触摸移动效果距离
||fabsf(deltaVector.y)>触摸、移动、效果、距离)
{
[自动停止触摸计时器];
}
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
UIView*touchView=[触摸视图];
CGPoint movedPoint=[触摸位置视图:self.view];
CGPoint deltaVector=CGPointMake(movedPoint.x-touchStartPoint.x,movedPoint.y-touchStartPoint.y);
if([touchView IsKindof类:[TestView类]]){
if(fabsf(deltaVector.x)
在touchBegin中获取touchStart时间,然后在touchMoved中检查与当前时间的时差。@codetiger这正是我想要避免的:我想知道调用touchesMoved之前的时间当您必须启动计时器时,该计时器每x millseconds检查时差,并在1秒后触发布尔值。