Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I also suggest taking a look at UISwipeGestureRecognizer, but if you prefer staying with your current design, this is the code that I am using elsewhere to detect swipes and pinch (zoom). You will need to adapt it to your case (you have three methods instead of three branches).</p> <pre><code> if (touch.phase == UITouchPhaseBegan) { // NSLog(@"TOUCH BEGAN"); _initialView = touchView; startTouchPosition1 = [touch locationInView:self]; startTouchTime = touch.timestamp; if ([allTouches count] &gt; 1) { startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; previousTouchPosition1 = startTouchPosition1; previousTouchPosition2 = startTouchPosition2; } } if (touch.phase == UITouchPhaseMoved) { // NSLog(@"TOUCH MOVED"); if ([allTouches count] &gt; 1) { CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2); CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2); if (fabs(currentFingerDistance - previousFingerDistance) &gt; ZOOM_DRAG_MIN) { NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance]; if (currentFingerDistance &gt; previousFingerDistance) { NSLog(@"zoom in"); [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance]; } else { NSLog(@"zoom out"); [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance]; } } } } if (touch.phase == UITouchPhaseEnded) { CGPoint currentTouchPosition = [touch locationInView:self]; // Check if it's a swipe if (fabsf(startTouchPosition1.x - currentTouchPosition.x) &gt;= SWIPE_DRAG_HORIZ_MIN &amp;&amp; // fabsf(startTouchPosition1.y - currentTouchPosition.y) &lt;= SWIPE_DRAG_VERT_MAX &amp;&amp; fabsf(startTouchPosition1.x - currentTouchPosition.x) &gt; fabsf(startTouchPosition1.y - currentTouchPosition.y) &amp;&amp; touch.timestamp - startTouchTime &lt; 0.7 ) { // It appears to be a swipe. if (startTouchPosition1.x &lt; currentTouchPosition.x) { NSLog(@"swipe right"); [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:touch]; } else { NSLog(@"swipe left"); [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:touch]; } } startTouchPosition1 = CGPointMake(-1, -1); _initialView = nil; } </code></pre> <p>EDIT: about your code...</p> <p>you are not doing a proper swipe detection. indeed, take this line:</p> <pre><code> if(CurrentPosition.x &gt;= kMinimumLength) </code></pre> <p>you are comparing a position (CurrentPosition.x) with a distance (kMinimumLength). This is not really meaningful. What you need saving the last touch position and then calculate the distance between the last touch and the current touch and compare this distance to kMinimumLenght.</p> <p>The reason why I posted the sample code is that, by inspecting a working implementation you can understand what needs to be done in order to detect a swipe.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload