Uipangesturerecognizer uplink/downlink sliding direction judgment Algorithm

Source: Internet
Author: User

Cgfloat const gestureminimumtranslation = 20.0;


Typedef Enum: nsinteger {

Kcameramovedirectionnone,

Kcameramovedirectionup,

Kcameramovedirectiondown,

Kcameramovedirectionright,

Kcameramovedireleft left

} Cameramovedirection;


@ Interfaceviewcontroller ()

{

Cameramovedirection direction;

}

@ End


@ Implementation viewcontroller


-(Void) viewdidload

{

[Super viewdidload];

Uipangesturerecognizer * recognizer = [[uipangesturerecognizer alloc] initwithtarget: Self action: @ selector (handleswipe :)];

[Self. viewwithgesturerecognizer addgesturerecognizer: recognizer];

}


// This Is My gesture recognizer handler, which detects movement in a particle

// Direction, conceptually tells a camera to start moving in that direction

// And when the user lifts their finger off the screen, tells the camera to stop.

-(Void) handleswipe :( uipangesturerecognizer *) gesture

{

Cgpoint translation = [gesture translationinview: Self. View];

If (gesture. State = uigesturerecognizerstatebegan)

{

Direction = kcameramovedirenone none;

}

Else if (gesture. State = uigesturerecognizerstatechanged & direction = kcameramovedirenone none)

{

Direction = [self determinecameradirectionifneeded: Translation];


// OK, now initiate movement in the direction indicated by the user's gesture

Switch (Direction ){

Case kcameramovedirectiondown:

Nslog (@ "start moving down ");

Break;

Case kcameramovedirectionup:

Nslog (@ "start moving up ");

Break;

Case kcameramovedirectionright:

Nslog (@ "start moving right ");

Break;

Case kcameramovedirectionleft:

Nslog (@ "start moving left ");

Break;

Default:

Break;

}

}

Elseif (gesture. State = uigesturerecognizerstateended)

{

// Now tell the camera to stop

Nslog (@ "stop ");

}

}


// This method will determine whether the direction of the user's swipe

-(Cameramovedirection) determinecameradirectionifneeded :( cgpoint) Translation

{

If (direction! = Kcameramovedirectionnone)

Return direction;

// Determine if horizontal swipe only if you meet some minimum velocity

If (FABS (translation. X)> gestureminimumtranslation)

{

Bool gesturehorizontal = no;

If (translation. Y = 0.0)

Gesturehorizontal = yes;

Else

Gesturehorizontal = (FABS (translation. X/translation. Y)> 5.0 );

If (gesturehorizontal)

{

If (translation. x> 0.0)

Return kcameramovedirectionright;

Else

Return kcameramovedirectionleft;

}

}

// Determine if vertical swipe only if you meet some minimum velocity

Elseif (FABS (translation. Y)> gestureminimumtranslation)

{

Bool gesturevertical = no;

If (translation. x = 0.0)

Gesturevertical = yes;

Else

Gesturevertical = (FABS (translation. Y/translation. X)> 5.0 );

If (gesturevertical)

{

If (translation. Y> 0.0)

Return kcameramovedirectiondown;

Else

Return kcameramovedirectionup;

}

}

Return direction;

}


@ End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.