iOS 自定义手势识别器测量长按的长度 [英] iOS Custom gesture recognizer measure length of longpress

查看:29
本文介绍了iOS 自定义手势识别器测量长按的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app i want to create a custom gesture recognizer that recognizes a long press followed by a swipe. I need to measure if the length of the long press is more than 1 second. If it is, then call a function and wait for the swipe action to begin.

我的问题是,我现在知道印刷机持续多长时间的唯一方法是从 touchesMoved 中提取 touchesBegan 的时间戳.但是我想知道在调用 touchesMoved 之前经过的时间.

My problem is that I the only way i know now how long the press was is by extracting the timestamp of touchesBegan from touchesMoved. However i want to know that elapsed time before touchesMoved gets called.

有没有办法在调用 touchesMoved 之前知道点击的长度?

Is there a way to know the length of the tap before the touchesMoved is called?

提前致谢!

推荐答案

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

you can use may code, it can use, but maybe you should deal with some details

在 .h 文件中,您应该添加这些 ivar:

in the .h file you should add these ivar:

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

    NSDate *touchStartTime;         
    CGPoint touchStartPoint;            
    CGPoint lastTouchPoint;

在 .m 文件中,您应该添加以下方法:

in the .m file you should add these method:

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


}

这篇关于iOS 自定义手势识别器测量长按的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆