代码改变世界

iOS 开发笔记(一)——iOS Gesture Recognizer 手势识别

2012-04-01 14:37  诸葛二牛  阅读(2776)  评论(0)    收藏  举报
在iOS 3.2之后 UIKit中添加了 UIGestureRecognizer Class 可以用来处理手势信息
  • 拍击 (任意次数的拍击)

  • 向里或向外捏 (用于缩放)

  • 摇动或者拖拽

  • 擦碰 (以任意方向)

  • 旋转 (手指朝相反方向移动)

  • 长按

     实现代码:

    1、一个手指,拍击两次手势

    // 创建一个手势识别器
    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];

    消息方法oneFingerTwoTaps
    - (void)oneFingerTwoTaps
    {
      NSLog(@"Action: One finger, two taps");
    }


    2、两个手指,拍击两次手势
    UITapGestureRecognizer *twoFingersTwoTaps = 
      [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
    [twoFingersTwoTaps setNumberOfTapsRequired:2];
    [twoFingersTwoTaps setNumberOfTouchesRequired:2];
    [[self view] addGestureRecognizer:twoFingersTwoTaps];

    消息方法twoFingersTwoTaps
    - (void)twoFingersTwoTaps {
      NSLog(@"Action: Two fingers, two taps");


    3、一个手指向上、向下擦碰手势
    // 向上擦碰
    UISwipeGestureRecognizer *oneFingerSwipeUp = 
      [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
    [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
    [[self view] addGestureRecognizer:oneFingerSwipeUp];

    - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 

      CGPoint point = [recognizer locationInView:[self view]];
      NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
    }

    // 向下擦碰
    UISwipeGestureRecognizer *oneFingerSwipeDown = 
      [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
    [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
    [[self view] addGestureRecognizer:oneFingerSwipeDown];

    - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer 

      CGPoint point = [recognizer locationInView:[self view]];
      NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
    }

    4、旋转手势
    UIRotationGestureRecognizer *twoFingersRotate = 
      [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
    [[self view] addGestureRecognizer:twoFingersRotate];

    - (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));
    }

    5、向里或向外捏的手势
    UIPinchGestureRecognizer *twoFingerPinch = 
      [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
    [[self view] addGestureRecognizer:twoFingerPinch];

    - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
    {
      NSLog(@"Pinch scale: %f", recognizer.scale);
    }

    高级手势识别器

Simultaneous Gesture Recognizers

Custom UIGestureRecognizer

继承于UIGestureRecognizer, 在一下方法中修改:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    UITouch * touch = [touches anyObject];
    self.curTickleStart = [touch locationInView:self.view];
}

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

}

- (void)reset {

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reset];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reset];
}

 

参考资料:
1 根据 Event Handling Guide for iOS
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html
2 子龙山人的博客 IOS 5手势识别教程
http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html