ios 为 UIGestureRecognizer 获取 UITouch 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4446831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:18:31  来源:igfitidea点击:

Getting the UITouch objects for a UIGestureRecognizer

objective-ccocoa-touchiosuigesturerecognizeruitouch

提问by Morrowless

Is there a way to get the UITouchobjects associated with a gesture? UIGestureRecognizerdoesn't seem to have any methods for this.

有没有办法获取UITouch与手势关联的对象?UIGestureRecognizer似乎没有任何方法可以做到这一点。

采纳答案by wpearse

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

杰是对的……你会想要一个子类。试试这个尺寸,它来自我的一个项目。在 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:

在 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];

}

回答by pfo

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.

如果只是您感兴趣的位置,则不必进行子类化,UIGestureRecognizer 会通知您点击的位置更改。

Initialize with target:

用目标初始化:

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

Handle UIGestureRecognizerStateChanged to get location changes:

处理 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;
    }
}

回答by neilkimmett

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.

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

回答by Jay

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

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

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

or the equivalent moved, ended or canceled

或等价物已移动、结束或取消

The Apple docshas lots of info on subclassing

苹果的文档具有大量的信息在子类

回答by MGM

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

这是一种将长按添加到任意 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.

这让你可以在任意数量的 UIViews 上运行 longpress。在本例中,我通过标签限制对 UIView 方法的访问,但您也可以使用 isKindOfClass。

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

(根据我的发现,您必须对 LongPress 使用 touchesMoved 因为 touchesBegan 在 LongPress 变为活动状态之前触发)

subclass - .h

子类 - .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

子类 - .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

视图控制器 - .h

                           //nothing special here

viewcontroller - .m

视图控制器 - .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
                    }
            }

回答by Timur Mustafaev

Simple and fast:

简单快速:

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

Where recognizer is your UIGestureRecognizer

识别器是你的 UIGestureRecognizer