如何自己的手势识别器滑动

时间:2014-03-06 14:07:27

标签: ios touch uigesturerecognizer swipe

我有一个问题..我想用滑动做我自己的手势识别器,然后用两根手指拖动(或滑动)但我现在不怎么做..

这是我的代码 GestureSwipe.h

#import <UIKit/UIKit.h>

@interface GestureSwipe : UIGestureRecognizer

@property CGPoint startTouchPosition;

@property(nonatomic) NSUInteger numberOfTouchesRequired;
@property(nonatomic) UISwipeGestureRecognizerDirection direction;


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

@end

这是我的代码 GestureSwipe.m

#define VERT_SWIPE_DRAG_MAX  7

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    // startTouchPosition is a property
    self.startTouchPosition = [aTouch locationInView:self.view];
}

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    CGPoint currentTouchPosition = [aTouch locationInView:self.view];
    //  Check if direction of touch is horizontal and long enough
    if (fabsf(self.startTouchPosition.y - currentTouchPosition.y) <=
        VERT_SWIPE_DRAG_MAX)
    {
        // If touch appears to be a swipe
        if (self.startTouchPosition.y < currentTouchPosition.y) {

            [self myProcessDownSwipe:touches withEvent:event];
        }
        self.startTouchPosition = CGPointZero;
    }
}

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

        self.startTouchPosition = CGPointZero;
}

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

}

我该怎么做才用两根手指滑动?

然后我有其他VC识别我自己的手势:

HelloViewController.m

- (void)viewDidLoad
{

    [super viewDidLoad];

    //newGesture = [[GestureSwipe alloc] init];

    newGesture = [[GestureSwipe alloc] initWithTarget:self action:@selector(showImage:)];

    newGesture.numberOfTouchesRequired = 2;


}

2 个答案:

答案 0 :(得分:0)

在HelloViewController中你可以添加如下内容:

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Set the acceptable swipe direction and number of touches required to do the gesture
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
recognizer.numberOfTouchesRequired = 2;

// Add the gesture recogizer to the view
[self.view addGestureRecognizer:recognizer];

然后你会创建一个自定义方法(我称之为一个handleSwipe:,但这可以是最适合你的方法)并做你需要做的事。

编辑:所以你的viewDidLoad方法看起来像这样

- (void)viewDidLoad
{

   [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer;

   recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Set the acceptable swipe direction and number of touches required to do the gesture
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    recognizer.numberOfTouchesRequired = 2;

    // Add the gesture recogizer to the view
    [self.view addGestureRecognizer:recognizer];


}

handleSwipe:方法可能是这样的:

-(void)handleSwipe:(id)sender{

    // Do something

 } 

答案 1 :(得分:0)

无需创建自己的识别器。您可以使用标准识别器处理此问题,并说明需要多少触摸。

可以在这里找到一个很好的例子:Capture only UIView 2 finger UIPanGestureRecognizer