1

我已经可以在 touchesMoved 中检测到滑动动作,但我想知道如何检测滑动,然后检测滑动停止但手指仍按在屏幕上的时间?

到目前为止,这是我的代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

if(self.tutorialView.alpha != 1.0 || self.tutorialView.hidden)
{

UITouch *touch = [touches anyObject];
CGPoint gestureEndPoint = [touch locationInView:self.view];

int dx = abs(gestureStartPoint.x - gestureEndPoint.x);
int dy = -1 * (gestureEndPoint.y - gestureStartPoint.y);

if(dx > 20) {
    // too much left/right, so don't do anything
    return;
}

if((gestureStartPoint.x - gestureEndPoint.x) < 20 && (gestureStartPoint.x - gestureEndPoint.x) > -20)
{

    if((gestureStartPoint.y - gestureEndPoint.y) > (gestureStartPoint.x - gestureEndPoint.x))
    {

        if(dy > 0)
        {

            // User has made an upwards slide motion

        }

        else
        {

            // User has made a downwards slide motion

    else
        self.number = 0;

    }

    }

}

}

}
4

2 回答 2

4

您可以使用计时器,在 中touchesMoved,使旧计时器无效并创建一个新计时器,这将触发“检测到停止运动”选择器。

像这样的东西

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     //NSTimer *_timer; // as an iver
    if (/* check move distance, if big enough */) {
        [_timer invalidate];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(slideStopped) userInfo:nil repeats:NO];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     [_timer invalidate]; // you may handle touch up differently and don't want slideStopped get called
     _timer = nil;
}

- (void)slideStopped {
    // handle slide stopped event
}
于 2013-02-09T13:14:18.753 回答
0

当运动停止时,将调用以下方法。所以你可以在这里跟踪它。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

于 2013-02-06T09:14:15.963 回答