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