2012-07-26 91 views
0

在我的应用程序中,我想创建一个自定义手势识别器,识别长按,然后刷卡。 我需要测量长按的长度是否超过1秒。如果是,则调用一个函数并等待滑动动作开始。iOS自定义手势识别器测量长按的长度

我的问题是,我唯一的方法,我知道现在多久的新闻是通过从touchesMoved提取touchesBegan的时间戳。不过,我想知道在touchesMoved被调用之前的流逝时间。

在touchesMoved被调用之前,有没有办法知道水龙头的长度?

在此先感谢!

+0

获取touchStart时间touchBegin,然后检查touchMoved中当前时间的差异。 – codetiger 2012-07-26 12:26:50

+0

@codetiger正是我想要避免的:我想知道touchesMoved被调用前的时间 – 2012-07-26 12:30:53

+0

然后,您必须启动一个计时器,该计时器每x毫秒检查一次时间差,并在一秒钟后触发布尔值。 – codetiger 2012-07-26 12:33:37

回答

2

可以使用可代码,它可以使用,但也许你应该应对.h文件的一些细节

你应该添加这些伊娃:在.m文件

TestView *aView ;//the view which you press 
    NSThread *timerThread;    
    NSTimer *touchTimer;  

    NSDate *touchStartTime;   
    CGPoint touchStartPoint;    
    CGPoint lastTouchPoint; 

你应该添加这些方法:

- (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]; 
      } 
     } 


} 
相关问题