Developer/iOS

[iPhone iPad] Gesture 인식 샘플

블로blow 2011. 5. 24. 09:44
728x90

- (void) touchesBegan:(NSSet *) touches withEvent: (UIEvent *) event
- (void) touchesMoved:(NSSet *) touches withEvent: (UIEvent *) event
- (void) touchesEnded:(NSSet *) touches withEvent: (UIEvent *) event
을 이용하여 인식을 한다.




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

{

finished = NO;

startPoint = [[touches anyObject] locationInView:self.view];

multitouch = (touches.count > 1);

pointCount = 1;

}


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

{

pointCount++;

if (finished) return;

// Handle multitouch

if (touches.count > 1)

{

// get touches

UITouch *touch1 = [[touches allObjects] objectAtIndex:0];

UITouch *touch2 = [[touches allObjects] objectAtIndex:1];

// find current and previous points

CGPoint cpoint1 = [touch1 locationInView:self.view];

CGPoint ppoint1 = [touch1 previousLocationInView:self.view];

CGPoint cpoint2 = [touch2 locationInView:self.view];

CGPoint ppoint2 = [touch2 previousLocationInView:self.view];

// calculate distances between the points

CGFloat cdist = distance(cpoint1, cpoint2);

CGFloat pdist = distance(ppoint1, ppoint2);

multitouch = YES;

        

// The pinch has to exceed a minimum distance

if (ABS(cdist - pdist) < MIN_PINCH) return;

if (cdist < pdist)

touchtype = UITouchPinchIn;

else

touchtype = UITouchPinchOut;

finished = YES;

return;

}

else 

{

// Check single touch for swipe

CGPoint cpoint = [[touches anyObject] locationInView:self.view];

float dx = DX(cpoint, startPoint);

float dy = DY(cpoint, startPoint);

multitouch = NO;

        

finished = YES;

if ((dx > SWIPE_DRAG_MIN) && (ABS(dy) < DRAGLIMIT_MAX)) // hswipe left

touchtype = UITouchSwipeLeft;

else if ((-dx > SWIPE_DRAG_MIN) && (ABS(dy) < DRAGLIMIT_MAX)) // hswipe right

touchtype = UITouchSwipeRight;

else if ((dy > SWIPE_DRAG_MIN) && (ABS(dx) < DRAGLIMIT_MAX)) // vswipe up

touchtype = UITouchSwipeUp;

else if ((-dy > SWIPE_DRAG_MIN) && (ABS(dx) < DRAGLIMIT_MAX)) // vswipe down

touchtype = UITouchSwipeDown;

else

finished = NO;

}

}


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

{

// was not detected as a swipe

if (!finished && !multitouch

{

// tap or double tap

if (pointCount < 3

{

if ([[touches anyObject] tapCount] == 1

touchtype = UITouchTap;

else

touchtype = UITouchDoubleTap;

}

else

touchtype = UITouchDrag;

}

// did points exceeded proper swipe?

if (finished && !multitouch

{

if (pointCount > POINT_TOLERANCE) touchtype = UITouchDrag;

}

// Is this properly a tap/double tap?

if (multitouch || (touches.count > 1))

{

// tolerance is *very* high

if (pointCount < 10)

{

if ([[touches anyObject] tapCount] == 1

touchtype = UITouchMultitouchTap;

else

touchtype = UITouchMultitouchDoubleTap;

}

}

NSString *whichItem = nil;

if (touchtype == UITouchUnknown) whichItem = @"Unknown";

else if (touchtype == UITouchTap) whichItem = @"Tap";

else if (touchtype == UITouchDoubleTap) whichItem = @"Double Tap";

else if (touchtype == UITouchDrag) whichItem = @"Drag";

else if (touchtype == UITouchMultitouchTap) whichItem = @"Multitouch Tap";

else if (touchtype == UITouchMultitouchDoubleTap) whichItem = @"Multitouch Double Tap";

else if (touchtype == UITouchSwipeLeft) whichItem = @"Swipe Left";

else if (touchtype == UITouchSwipeRight) whichItem = @"Swipe Right";

else if (touchtype == UITouchSwipeUp) whichItem = @"Swipe Up";

else if (touchtype == UITouchSwipeDown) whichItem = @"Swipe Down";

else if (touchtype == UITouchPinchIn) whichItem = @"Pinch In";

else if (touchtype == UITouchPinchOut) whichItem = @"Pinch Out";

    

//[self.vc performSelector:@selector(updateState:withPoints:) withObject:whichItem withObject:[NSNumber numberWithInt:pointCount]];

    

    [self performSelector:@selector(updateState:pointCount:) withObject:whichItem withObject:[NSNumber numberWithInt:pointCount]];

       


}


-(void)updateState:(NSString*)state pointCount:(NSNumber*)count

{

    NSString *outstring = [NSString stringWithFormat:@"state = %@, count = %d", state, [count intValue]];

UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"Gestures" message:outstring delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil] autorelease];

[av show];

}

728x90