获取 UIGestureRecognizer 的 UITouch 对象

发布于 2024-10-07 12:17:06 字数 94 浏览 0 评论 0原文

有没有办法获取与手势关联的 UITouch 对象? UIGestureRecognizer 似乎没有任何方法来实现这一点。

Is there a way to get the UITouch objects associated with a gesture? UIGestureRecognizer doesn't seem to have any methods for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

往事随风而去 2024-10-14 12:17:06

杰伊是对的......你会想要一个子类。试试这个尺寸,它来自我的一个项目。在 DragGestureRecognizer.h 中:

@interface DragGestureRecognizer : UILongPressGestureRecognizer {

}

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

@end

@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
@end

在 DragGestureRecognizer.m 中:

#import "DragGestureRecognizer.h"


@implementation DragGestureRecognizer

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

    [super touchesMoved:touches withEvent:event];

    if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
        [(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
    }

}

@end 

当然,您需要

- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event; 

在委托中实现该方法 - 例如:

DragGestureRecognizer * gr = [[DragGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
gr.minimumPressDuration = 0.15;
gr.delegate = self;
[self.view addGestureRecognizer:gr];
[gr release];



- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{

    UITouch * touch = [touches anyObject];
    self.mTouchPoint = [touch locationInView:self.view];
    self.mFingerCount = [touches count];

}

Jay's right... you'll want a subclass. Try this one for size, it's from one of my projects. In DragGestureRecognizer.h:

@interface DragGestureRecognizer : UILongPressGestureRecognizer {

}

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

@end

@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
@end

And in DragGestureRecognizer.m:

#import "DragGestureRecognizer.h"


@implementation DragGestureRecognizer

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

    [super touchesMoved:touches withEvent:event];

    if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
        [(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
    }

}

@end 

Of course, you'll need to implement the

- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event; 

method in your delegate -- for example:

DragGestureRecognizer * gr = [[DragGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
gr.minimumPressDuration = 0.15;
gr.delegate = self;
[self.view addGestureRecognizer:gr];
[gr release];



- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{

    UITouch * touch = [touches anyObject];
    self.mTouchPoint = [touch locationInView:self.view];
    self.mFingerCount = [touches count];

}
独孤求败 2024-10-14 12:17:06

如果它只是您感兴趣的位置,则不必子类化,您将收到 UIGestureRecognizer 中点击位置更改的通知。

使用目标进行初始化:

self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];

处理 UIGestureRecognizerStateChanged 以获取位置更改:

- (void)handleGesture: (UIGestureRecognizer *)theGestureRecognizer {
    switch (theGestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
        {
            CGPoint location = [theGestureRecognizer locationInView: self.tableView];

            [self infoForLocation: location];

            break;
        }

        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"Ended");

            break;
        }

        default:
            break;
    }
}

If it is just the location you are interested in, you do not have to subclass, you will be notified of location changes of the tap from the UIGestureRecognizer.

Initialize with target:

self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];

Handle UIGestureRecognizerStateChanged to get location changes:

- (void)handleGesture: (UIGestureRecognizer *)theGestureRecognizer {
    switch (theGestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
        {
            CGPoint location = [theGestureRecognizer locationInView: self.tableView];

            [self infoForLocation: location];

            break;
        }

        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"Ended");

            break;
        }

        default:
            break;
    }
}
情丝乱 2024-10-14 12:17:06

如果您只需要找出手势的位置,则可以在 UIGestureRecognizer 对象上调用 locationInView: 或 locationOfTouch:inView: 。但是,如果您想做其他任何事情,那么您需要子类化。

If you only need to find out the location of the gesture, you can call either locationInView: or locationOfTouch:inView: on the UIGestureRecognizer object. However if you want to do anything else, then you'll need to subclass.

山色无中 2024-10-14 12:17:06

如果您正在编写自己的 UIGestureRecognizer,则可以覆盖触摸对象:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

或等效的移动、结束或取消

Apple 文档 有很多关于子类化的信息

If you're writing your own UIGestureRecognizer you can get the touch objects overriding:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

or the equivalent moved, ended or canceled

The Apple docs has lots of info on subclassing

长安忆 2024-10-14 12:17:06

这是一种将长按添加到任意 UIView 的方法。

这使您可以在任意数量的 UIView 上运行长按。
在此示例中,我通过标签限制对 UIView 方法的访问,但您也可以使用 isKindOfClass。

(据我发现,您必须对 LongPress 使用 TouchMoved,因为在 LongPress 激活之前,touchesBegan 会触发)

subclass - .h

            #import <UIKit/UIKit.h>
            #import <UIKit/UIGestureRecognizerSubclass.h>

            @interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer

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

            @property (nonatomic) CGPoint touchPoint;
            @property (strong, nonatomic) UIView* touchView;

            @end

subclass - .m

            #import "MyLongPress.h"

            @implementation MyLongPressGestureRecognizer

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

            #pragma mark - Setters

            -(void) setTouchPoint:(CGPoint)touchPoint
            {
                _touchPoint =touchPoint;
            }

            -(void) setTouchView:(UIView*)touchView
            {
                _touchView=touchView;
            }
            @end

viewcontroller - .h

                           //nothing special here

viewcontroller - .m

            #import "ViewController.h"
            //LOAD
            #import "MyLongPress.h"

            @interface ViewController ()

            //lOAD
            @property (strong, nonatomic) MyLongPressGestureRecognizer* longPressGesture;

            @end

            @implementation PDViewController

            - (void)viewDidLoad
            {
                [super viewDidLoad];

                //LOAD
                self.longPressGesture =[[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
                self.longPressGesture.minimumPressDuration = 1.2;
                [[self view] addGestureRecognizer:self.longPressGesture];
            }               

            //LOAD
            -(void) setLongPressGesture:(MyLongPressGestureRecognizer *)longPressGesture {
                _longPressGesture = longPressGesture;
            }

            - (void)longPressHandler:(MyLongPressGestureRecognizer *)recognizer {
                    if (self.longPressGesture.touchView.tag >= 100) /* arbitrary method for limiting tap */
                    {
                        //code goes here
                    }
            }

Here is a method to get a long press added to an arbitrary UIView.

This lets you run longpress on any number of UIViews.
In this example I restrict access to the UIView method through tags but you could use isKindOfClass as well.

(From what I found you have to use touchesMoved for LongPress because touchesBegan fires before the LongPress becomes active)

subclass - .h

            #import <UIKit/UIKit.h>
            #import <UIKit/UIGestureRecognizerSubclass.h>

            @interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer

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

            @property (nonatomic) CGPoint touchPoint;
            @property (strong, nonatomic) UIView* touchView;

            @end

subclass - .m

            #import "MyLongPress.h"

            @implementation MyLongPressGestureRecognizer

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

            #pragma mark - Setters

            -(void) setTouchPoint:(CGPoint)touchPoint
            {
                _touchPoint =touchPoint;
            }

            -(void) setTouchView:(UIView*)touchView
            {
                _touchView=touchView;
            }
            @end

viewcontroller - .h

                           //nothing special here

viewcontroller - .m

            #import "ViewController.h"
            //LOAD
            #import "MyLongPress.h"

            @interface ViewController ()

            //lOAD
            @property (strong, nonatomic) MyLongPressGestureRecognizer* longPressGesture;

            @end

            @implementation PDViewController

            - (void)viewDidLoad
            {
                [super viewDidLoad];

                //LOAD
                self.longPressGesture =[[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
                self.longPressGesture.minimumPressDuration = 1.2;
                [[self view] addGestureRecognizer:self.longPressGesture];
            }               

            //LOAD
            -(void) setLongPressGesture:(MyLongPressGestureRecognizer *)longPressGesture {
                _longPressGesture = longPressGesture;
            }

            - (void)longPressHandler:(MyLongPressGestureRecognizer *)recognizer {
                    if (self.longPressGesture.touchView.tag >= 100) /* arbitrary method for limiting tap */
                    {
                        //code goes here
                    }
            }
土豪 2024-10-14 12:17:06

简单快速:

NSArray *touches = [recognizer valueForKey:@"touches"];

识别器就是您的 UIGestureRecognizer

Simple and fast:

NSArray *touches = [recognizer valueForKey:@"touches"];

Where recognizer is your UIGestureRecognizer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文