在touchesMoved中,如何检测滑动运动然后停止?

时间:2013-02-06 08:01:33

标签: iphone ios objective-c touch touchesmoved

我已经可以在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;

    }

    }

}

}

}

2 个答案:

答案 0 :(得分:4)

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

类似这样的事情

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

答案 1 :(得分:0)

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

- (void)touchesEnded:(NSSet *)触及withEvent:(UIEvent *)事件