繁体   English   中英

自己的手势识别器如何刷卡

[英]How do own Gesture Recognizer 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;


}

在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

 } 

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

可以在此处找到一个很好的示例: 仅捕获UIView 2手指UIPanGestureRecognizer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM