본문 바로가기

개발도구/iOS - 아이폰 개발

[iOS]GestureRecognizer 터치이벤트 와 테이블에 적용방법

먼저 테이블의 적용은 이렇다

 UITapGestureRecognizer *TapPressed = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onCellTapPressed:)];

    [self.tableviewname addGestureRecognizer:TapPressed];

    [TapPressed release];

아래의 설명에 따라 제스쳐를 달아주고 위와 같이 테이블에 addGestureRecognizer로 적용하게 되면 처리 완료!!


The UIGestureRecognizer class is available to help with detecting and responding to the various UI gestures common on iOS devices. UIGestureRecognizer is an abstract class, with the following concrete subclasses, one for each type of available recognizer:

  1. UITapGestureRecognizer
  2. UIPinchGestureRecognizer
  3. UIRotationGestureRecognizer
  4. UISwipeGestureRecognizer
  5. UIPanGestureRecognizer
  6. UILongPressGestureRecognizer


Depending on the type of recognizer, there are various behaviors that you can configure. For instance, with the UITapGestureRecognizer, you can specify the number of taps and number of touches. As an example, you could specify that two fingers are required, tapping twice with both fingers.

In response to recognized gestures, an action messages is sent to a target object that you specify. Depending on the type of gesture additional information about the gesture may be available in the action method, for example, the start location of a swipe, or the scale factor of a pinch/zoom.

Once you’ve defined a gesture, you add the recognizer to the view. From there, the view is responsible for recognizing when a gesture has occurrred and calling the relevant action.

Gesture Recognizer Examples

Let’s walk through configuration of a few gestures to see how all this works.

Below I’ve defined a recognizer for managing taps, notice how I’ve specified that there are two taps required, with just one touch (finger). The @selector specifies the method that will be called when the gesture is recognized.

// -----------------------------

// One finger, two taps

// -----------------------------

 

// Create gesture recognizer

UITapGestureRecognizer *oneFingerTwoTaps = 

  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];

 

// Set required taps and number of touches

[oneFingerTwoTaps setNumberOfTapsRequired:2];

[oneFingerTwoTaps setNumberOfTouchesRequired:1];

 

// Add the gesture to the view

[[self view] addGestureRecognizer:oneFingerTwoTaps];

Below is a recognizer for two fingers, two taps:

// -----------------------------

// Two fingers, two taps

// -----------------------------

UITapGestureRecognizer *twoFingersTwoTaps = 

  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];

[twoFingersTwoTaps setNumberOfTapsRequired:2];

[twoFingersTwoTaps setNumberOfTouchesRequired:2];

[[self view] addGestureRecognizer:twoFingersTwoTaps];

The following gestures recognize swipes up and down:

// -----------------------------

// One finger, swipe up

// -----------------------------

UISwipeGestureRecognizer *oneFingerSwipeUp = 

  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];

[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];

[[self view] addGestureRecognizer:oneFingerSwipeUp];

 

// -----------------------------

// One finger, swipe down

// -----------------------------

UISwipeGestureRecognizer *oneFingerSwipeDown = 

  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];

[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];

[[self view] addGestureRecognizer:oneFingerSwipeDown];

Two recognize two fingers rotating on a view:

// -----------------------------

// Two finger rotate  

// -----------------------------

UIRotationGestureRecognizer *twoFingersRotate = 

  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];

[[self view] addGestureRecognizer:twoFingersRotate];

And finally, two finger pinch:

// -----------------------------

// Two finger pinch

// -----------------------------

UIPinchGestureRecognizer *twoFingerPinch = 

  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];

There are two additional gestures not shown above that you can also tinker with,UIPanGestureRecognizer and UILongPressGestureRecognizer.

Gesture Recognizer Action Methods

For each of the above definitions, below are the associated action methods.

/*--------------------------------------------------------------

* One finger, two taps 

*-------------------------------------------------------------*/

- (void)oneFingerTwoTaps

{

  NSLog(@"Action: One finger, two taps");

}

 

/*--------------------------------------------------------------

* Two fingers, two taps

*-------------------------------------------------------------*/

- (void)twoFingersTwoTaps {

  NSLog(@"Action: Two fingers, two taps");

} 

 

/*--------------------------------------------------------------

* One finger, swipe up

*-------------------------------------------------------------*/

- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 

{ 

  CGPoint point = [recognizer locationInView:[self view]];

  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);

}

 

/*--------------------------------------------------------------

* One finger, swipe down

*-------------------------------------------------------------*/

- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer 

{ 

  CGPoint point = [recognizer locationInView:[self view]];

  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);

}

 

/*--------------------------------------------------------------

* Two finger rotate   

*-------------------------------------------------------------*/

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 

{

  // Convert the radian value to show the degree of rotation

  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));

}

 

/*--------------------------------------------------------------

* Two finger pinch

*-------------------------------------------------------------*/

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 

{

  NSLog(@"Pinch scale: %f", recognizer.scale);

}

Source Code


XcodeGestureRecognizers.zip


To get a feel for how gestures and the corresponding action methods work, download the Xcode project below and install the application on a device from within Xcode – look at the console output as you perform the various gestures to see the log messages for each gesture as it’s recognized.