0

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

我的问题是,我现在知道的唯一方法是从 touchesMoved 中提取 touchesBegan 的时间戳。但是我想知道在调用 touchesMoved 之前经过的时间。

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

提前致谢!

4

1 回答 1

2

你可以使用可能的代码,它可以使用,但也许你应该处理一些细节

在 .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];
            }
        }


}
于 2012-07-26T13:03:00.433 回答