iOS手勢識別的詳細使用(拖動,縮放,旋轉,點選,手勢依賴,自定義手勢)

benbenxiongyuan發表於2014-02-10

1、UIGestureRecognizer介紹

手勢識別在iOS上非常重要,手勢操作移動裝置的重要特徵,極大的增加了移動裝置使用便捷性。
iOS系統在3.2以後,為方便開發這使用一些常用的手勢,提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個抽象類,下面的子類是具體的手勢,開發這可以直接使用這些手勢識別。
  • Tap(點一下)
  • Pinch(二指往內或往外撥動,平時經常用到的縮放)
  • Rotation(旋轉)
  • Swipe(滑動,快速移動)
  • Pan (拖移,慢速移動)
  •  LongPress(長按)
UIGestureRecognizer的繼承關係如下:



2、使用手勢的步驟

使用手勢很簡單,分為兩步:
  1. 建立手勢例項。當建立手勢時,指定一個回撥方法,當手勢開始,改變、或結束時,回撥方法被呼叫。
  2. 新增到需要識別的View中。每個手勢只對應一個View,當螢幕觸控在View的邊界內時,如果手勢和預定的一樣,那就會回撥方法。
ps:一個手勢只能對應一個View,但是一個View可以有多個手勢。
建議在真機上執行這些手勢,模擬器操作不太方便,可能導致你認為手勢失效。

3、Pan 拖動手勢:

  1. UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];  
  2. snakeImageView.frame = CGRectMake(50, 50, 100, 160);  
  3. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]  
  4.                                                 initWithTarget:self  
  5.                                                 action:@selector(handlePan:)];      
  6. [snakeImageView addGestureRecognizer:panGestureRecognizer];  
  7. [self.view setBackgroundColor:[UIColor whiteColor]];  
  8. [self.view addSubview:snakeImageView];  
    UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];
    snakeImageView.frame = CGRectMake(50, 50, 100, 160);
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(handlePan:)];    
    [snakeImageView addGestureRecognizer:panGestureRecognizer];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:snakeImageView];
新建一個ImageView,然後新增手勢
回撥方法:
  1. - (void) handlePan:(UIPanGestureRecognizer*) recognizer  
  2. {  
  3.     CGPoint translation = [recognizer translationInView:self.view];  
  4.     recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,  
  5.                                    recognizer.view.center.y + translation.y);  
  6.     [recognizer setTranslation:CGPointZero inView:self.view];  
  7.       
  8. }  
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                   recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
    
}

4、Pinch縮放手勢

  1. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]  
  2.                                                         initWithTarget:self  
  3.                                                         action:@selector(handlePinch:)];<P class=p1>[<SPAN class=s1>snakeImageView</SPAN> <SPAN class=s2>addGestureRecognizer</SPAN>:pinchGestureRecognizer];</P>  
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePinch:)];

[snakeImageView addGestureRecognizer:pinchGestureRecognizer];


  1. - (void) handlePinch:(UIPinchGestureRecognizer*) recognizer  
  2. {  
  3.     recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);  
  4.     recognizer.scale = 1;  
  5. }  
- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
{
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}

5、Rotation旋轉手勢

  1. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]  
  2.                                                  initWithTarget:self  
  3.                                                  action:@selector(handleRotate:)];  
  4. [snakeImageView addGestureRecognizer:rotateRecognizer];  
    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
                                                     initWithTarget:self
                                                     action:@selector(handleRotate:)];
    [snakeImageView addGestureRecognizer:rotateRecognizer];

  1. - (void) handleRotate:(UIRotationGestureRecognizer*) recognizer  
  2. {  
  3.     recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);  
  4.     recognizer.rotation = 0;  
  5. }  
- (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
{
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0;
}


 

新增了這幾個手勢後,執行看效果,程式中的imageView放了一個
                    /^\/^\
                  _|__|  O|
         \/     /~     \_/ \
          \____|__________/  \
                 \_______      \
                         `\     \                 \
                           |     |                  \
                          /      /                    \
                         /     /                       \\
                       /      /                         \ \
                      /     /                            \  \
                    /     /             _----_            \   \
                   /     /           _-~      ~-_         |   |
                  (      (        _-~    _--_    ~-_     _/   |
                   \      ~-____-~    _-~    ~-_    ~-_-~    /
                     ~-_           _-~          ~-_       _-~  
                        ~--______-~                ~-___-~
的圖片,在模擬器上拖動是沒問題的。縮放和旋轉有點問題,估計是因為在模擬器上的模擬的兩個接觸點距離在imageView的邊界外了,所以操作無效果。
建議在真機上執行這個手勢。
在模擬器上縮放和選擇的操作技巧:
可以把imageView的frame值設定大一點,按住alt鍵,按下觸控板(不按下不行),這樣就可以旋轉和縮放了。

6、新增第二個ImagView並新增手勢

記住:一個手勢只能新增到一個View,兩個View當然要有兩個手勢的例項了
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];  
  6.     UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dragon.png"]];  
  7.     snakeImageView.frame = CGRectMake(120, 120, 100, 160);  
  8.     dragonImageView.frame = CGRectMake(50, 50, 100, 160);  
  9.     [self.view addSubview:snakeImageView];  
  10.     [self.view addSubview:dragonImageView];  
  11.       
  12.     for (UIView *view in self.view.subviews) {  
  13.         UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]  
  14.                                                         initWithTarget:self  
  15.                                                         action:@selector(handlePan:)];  
  16.           
  17.         UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]  
  18.                                                             initWithTarget:self  
  19.                                                             action:@selector(handlePinch:)];  
  20.           
  21.         UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]  
  22.                                                          initWithTarget:self  
  23.                                                          action:@selector(handleRotate:)];  
  24.           
  25.         [view addGestureRecognizer:panGestureRecognizer];  
  26.         [view addGestureRecognizer:pinchGestureRecognizer];  
  27.         [view addGestureRecognizer:rotateRecognizer];  
  28.         [view setUserInteractionEnabled:YES];  
  29.     }  
  30.     [self.view setBackgroundColor:[UIColor whiteColor]];       
  31. }  
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];
    UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dragon.png"]];
    snakeImageView.frame = CGRectMake(120, 120, 100, 160);
    dragonImageView.frame = CGRectMake(50, 50, 100, 160);
    [self.view addSubview:snakeImageView];
    [self.view addSubview:dragonImageView];
    
    for (UIView *view in self.view.subviews) {
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePan:)];
        
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
                                                            initWithTarget:self
                                                            action:@selector(handlePinch:)];
        
        UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
                                                         initWithTarget:self
                                                         action:@selector(handleRotate:)];
        
        [view addGestureRecognizer:panGestureRecognizer];
        [view addGestureRecognizer:pinchGestureRecognizer];
        [view addGestureRecognizer:rotateRecognizer];
        [view setUserInteractionEnabled:YES];
    }
    [self.view setBackgroundColor:[UIColor whiteColor]];     
}

多新增了一條龍的view,兩個view都能接收上面的三種手勢。執行效果如下:


7、拖動(pan手勢)速度(以較快的速度拖放後view有滑行的效果)

如何實現呢?
  1. 監視手勢是否結束
  2. 監視觸控的速度
  1. - (void) handlePan:(UIPanGestureRecognizer*) recognizer  
  2. {  
  3.     CGPoint translation = [recognizer translationInView:self.view];  
  4.     recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,  
  5.                                        recognizer.view.center.y + translation.y);  
  6.     [recognizer setTranslation:CGPointZero inView:self.view];  
  7.       
  8.     if (recognizer.state == UIGestureRecognizerStateEnded) {  
  9.           
  10.         CGPoint velocity = [recognizer velocityInView:self.view];  
  11.         CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));  
  12.         CGFloat slideMult = magnitude / 200;  
  13.         NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);  
  14.           
  15.         float slideFactor = 0.1 * slideMult; // Increase for more of a slide   
  16.         CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),  
  17.                                          recognizer.view.center.y + (velocity.y * slideFactor));  
  18.         finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);  
  19.         finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);  
  20.           
  21.         [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{  
  22.             recognizer.view.center = finalPoint;  
  23.         } completion:nil];  
  24.           
  25.     }  
  26.      
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                       recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        CGPoint velocity = [recognizer velocityInView:self.view];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;
        NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
        
        float slideFactor = 0.1 * slideMult; // Increase for more of a slide
        CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
                                         recognizer.view.center.y + (velocity.y * slideFactor));
        finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
        finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
        
        [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            recognizer.view.center = finalPoint;
        } completion:nil];
        
    }
   
程式碼實現解析:
  1. 計算速度向量的長度(估計大部分都忘了)這些知識了。
  2. 如果速度向量小於200,那就會得到一個小於的小數,那麼滑行會很短
  3. 基於速度和速度因素計算一個終點
  4. 確保終點不會跑出父View的邊界
  5. 使用UIView動畫使view滑動到終點
執行後,快速拖動影像view放開會看到view還會在原來的方向滑行一段路。

8、同時觸發兩個view的手勢

手勢之間是互斥的,如果你想同時觸發蛇和龍的view,那麼需要實現協議

UIGestureRecognizerDelegate,

  1. @interface ViewController : UIViewController<UIGestureRecognizerDelegate>  
  2. @end  
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
@end
並在協議這個方法裡返回YES。
  1. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  
  2. {  
  3.     return YES;  
  4. }  
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
把self作為代理設定給手勢:

  1. panGestureRecognizer.delegate = self;  
  2. pinchGestureRecognizer.delegate = self;  
  3. rotateRecognizer.delegate = self;  
        panGestureRecognizer.delegate = self;
        pinchGestureRecognizer.delegate = self;
        rotateRecognizer.delegate = self;
這樣可以同時拖動或旋轉縮放兩個view了。

9、tap點選手勢

這裡為了方便看到tap的效果,當點選一下螢幕時,播放一個聲音。

為了播放聲音,我們加入AVFoundation.framework這個框架。

  1. - (AVAudioPlayer *)loadWav:(NSString *)filename {  
  2.     NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"];  
  3.     NSError * error;  
  4.     AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];  
  5.     if (!player) {  
  6.         NSLog(@"Error loading %@: %@", url, error.localizedDescription);  
  7.     } else {  
  8.         [player prepareToPlay];  
  9.     }  
  10.     return player;  
  11. }  
- (AVAudioPlayer *)loadWav:(NSString *)filename {
    NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"];
    NSError * error;
    AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (!player) {
        NSLog(@"Error loading %@: %@", url, error.localizedDescription);
    } else {
        [player prepareToPlay];
    }
    return player;
}
我會在最後例子程式碼給出完整程式碼,新增手勢的步驟和前面一樣的。

  1. #import <UIKit/UIKit.h>   
  2. #import <AVFoundation/AVFoundation.h>   
  3.   
  4. @interface ViewController : UIViewController<UIGestureRecognizerDelegate>  
  5. @property (strong) AVAudioPlayer * chompPlayer;  
  6. @property (strong) AVAudioPlayer * hehePlayer;  
  7.   
  8. @end  
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
@property (strong) AVAudioPlayer * chompPlayer;
@property (strong) AVAudioPlayer * hehePlayer;

@end

  1. - (void)handleTap:(UITapGestureRecognizer *)recognizer {  
  2.     [self.chompPlayer play];  
  3. }  
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    [self.chompPlayer play];
}

執行,點一下某個圖,就會播放一個咬東西的聲音。

不過這個點選播放聲音有點缺陷,就是在慢慢拖動的時候也會播放。這使得兩個手勢重合了。怎麼解決呢?使用手勢的:requireGestureRecognizerToFail方法。

10、手勢的依賴性

在viewDidLoad的迴圈裡新增這段程式碼:

  1. [tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];  
        [tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];
意思就是,當如果pan手勢失敗,就是沒發生拖動,才會出發tap手勢。這樣如果你有輕微的拖動,那就是pan手勢發生了。tap的聲音就不會發出來了。


11、自定義手勢

自定義手勢繼承:UIGestureRecognizer,實現下面的方法:

  1. – touchesBegan:withEvent:  
  2. – touchesMoved:withEvent:  
  3. – touchesEnded:withEvent:  
  4. - touchesCancelled:withEvent:  
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent:

新建一個類,繼承UIGestureRecognizer,程式碼如下:

.h檔案

  1. #import <UIKit/UIKit.h>   
  2. typedef enum {  
  3.     DirectionUnknown = 0,  
  4.     DirectionLeft,  
  5.     DirectionRight  
  6. } Direction;  
  7.   
  8. @interface HappyGestureRecognizer : UIGestureRecognizer  
  9. @property (assign) int tickleCount;  
  10. @property (assign) CGPoint curTickleStart;  
  11. @property (assign) Direction lastDirection;  
  12.   
  13. @end  
#import <UIKit/UIKit.h>
typedef enum {
    DirectionUnknown = 0,
    DirectionLeft,
    DirectionRight
} Direction;

@interface HappyGestureRecognizer : UIGestureRecognizer
@property (assign) int tickleCount;
@property (assign) CGPoint curTickleStart;
@property (assign) Direction lastDirection;

@end
.m檔案

  1. #import "HappyGestureRecognizer.h"   
  2. #import <UIKit/UIGestureRecognizerSubclass.h>   
  3. #define REQUIRED_TICKLES        2   
  4. #define MOVE_AMT_PER_TICKLE     25   
  5.   
  6. @implementation HappyGestureRecognizer  
  7.   
  8. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  9.     UITouch * touch = [touches anyObject];  
  10.     self.curTickleStart = [touch locationInView:self.view];  
  11. }  
  12.   
  13. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
  14.       
  15.     // Make sure we've moved a minimum amount since curTickleStart   
  16.     UITouch * touch = [touches anyObject];  
  17.     CGPoint ticklePoint = [touch locationInView:self.view];  
  18.     CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;  
  19.     Direction curDirection;  
  20.     if (moveAmt < 0) {  
  21.         curDirection = DirectionLeft;  
  22.     } else {  
  23.         curDirection = DirectionRight;  
  24.     }  
  25.     if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;  
  26.       
  27.     // 確認方向改變了   
  28.     if (self.lastDirection == DirectionUnknown ||  
  29.         (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||  
  30.         (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {  
  31.           
  32.         // 撓癢次數   
  33.         self.tickleCount++;  
  34.         self.curTickleStart = ticklePoint;  
  35.         self.lastDirection = curDirection;  
  36.           
  37.         // 一旦撓癢次數超過指定數,設定手勢為結束狀態   
  38.         // 這樣回撥函式會被呼叫。   
  39.         if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {  
  40.             [self setState:UIGestureRecognizerStateEnded];  
  41.         }  
  42.     }  
  43.       
  44. }  
  45.   
  46. - (void)reset {  
  47.     self.tickleCount = 0;  
  48.     self.curTickleStart = CGPointZero;  
  49.     self.lastDirection = DirectionUnknown;  
  50.     if (self.state == UIGestureRecognizerStatePossible) {  
  51.         [self setState:UIGestureRecognizerStateFailed];  
  52.     }  
  53. }  
  54.   
  55. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  56. {  
  57.     [self reset];  
  58. }  
  59.   
  60. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
  61. {  
  62.     [self reset];  
  63. }  
  64.   
  65. @end  
#import "HappyGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#define REQUIRED_TICKLES        2
#define MOVE_AMT_PER_TICKLE     25

@implementation HappyGestureRecognizer

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    // Make sure we've moved a minimum amount since curTickleStart
    UITouch * touch = [touches anyObject];
    CGPoint ticklePoint = [touch locationInView:self.view];
    CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
    Direction curDirection;
    if (moveAmt < 0) {
        curDirection = DirectionLeft;
    } else {
        curDirection = DirectionRight;
    }
    if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
    
    // 確認方向改變了
    if (self.lastDirection == DirectionUnknown ||
        (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
        (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
        
        // 撓癢次數
        self.tickleCount++;
        self.curTickleStart = ticklePoint;
        self.lastDirection = curDirection;
        
        // 一旦撓癢次數超過指定數,設定手勢為結束狀態
        // 這樣回撥函式會被呼叫。
        if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
            [self setState:UIGestureRecognizerStateEnded];
        }
    }
    
}

- (void)reset {
    self.tickleCount = 0;
    self.curTickleStart = CGPointZero;
    self.lastDirection = DirectionUnknown;
    if (self.state == UIGestureRecognizerStatePossible) {
        [self setState:UIGestureRecognizerStateFailed];
    }
}

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

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

@end

呼叫自定義手勢和上面一樣,回到這樣寫:

  1. - (void)handleHappy:(HappyGestureRecognizer *)recognizer{  
  2.     [self.hehePlayer play];  
  3. }  
- (void)handleHappy:(HappyGestureRecognizer *)recognizer{
    [self.hehePlayer play];
}
手勢成功後播放呵呵笑的聲音。
在真機上執行,按住某個view,快速左右拖動,就會發出笑的聲音了。

程式碼解析:
先獲取起始座標:curTickleStart
通過和ticklePoint的x值對比,得出當前的放下是向左還是向右。再算出移動的x的值是否比MOVE_AMT_PER_TICKLE距離大,如果太則返回。
再判斷是否有三次是不同方向的動作,如果是則手勢結束,回撥。

參考:http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

容芳志 (http://blog.csdn.net/totogo2010)

本文遵循“署名-非商業用途-保持一致”創作公用協議

相關文章