2012-07-26 145 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]; 
      } 
     } 


} 
相關問題