justicepenny
justicepenny

Reputation: 2024

using touchesbegan and touchesmoved to distinguish flick and drag

i use these two functions to detect the user slowly drag and drop on uiview

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

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

however, how can i use these two methods to detect user actually flick(quick flip) uiview ?

how can i distinguish flick from drag?

thanks a lot for your help!

justicepenny

Upvotes: 1

Views: 785

Answers (3)

Guo Luchuan
Guo Luchuan

Reputation: 4731

you can have a try like this use -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method to mark the beginPoint and beginTime; ues -(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event method to mark the endPoint and endTime . and then calc the speed , you can compare the speed and your threshold.(The speed maybe only calc Horizontal or Vertical)

Upvotes: 4

Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

Drag and Flick are usually distinguished by speed - one solution would be to create an algorithm based on the distance formula.

One rough example:

CGPoint pointOld = CGPointMake(0, 0); // Not sure if this is valid
CGPoint pointNew = CGPointMate(0, 0); // Just making holders for the
                                      // location of the current and previous touches

float timeInterval = 0.2f;
// However long you think it will take to have enough of a difference in
// distance to distinguish a flick from a drag

float minFlickDist = 100.0f;
// Minimum distance traveled in timeInterval to be considered a flick

- (void)callMeEveryTimeInterval
{
    // Distance formula
    float distBtwnPoints = sqrt( (pointNew.x - pointOld.x) * (pointNew.x - pointOld.x) - (pointNew.y - pointOld.y) * (pointNew.y - pointOld.y) );
    if (distBtwnPoints >= minFlickDist)
    {
        // Flick
    } else {
        // Drag
    }
}

Really rough sketch of something I think might work - hope that helps.

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

I think you should check out gesture recognizers -- they take a lot of the work out of distinguishing between different user touches. What you're describing are pan and swipe gestures. There are specific gesture recognizer classes to deal with each of those. UIGestureRecognizer is the parent class, you should look at that first.

Upvotes: 1

Related Questions