获取UIGestureRecognizer的UITouch对象

19
有没有一种方法可以获取与手势相关联的 UITouch 对象?UIGestureRecognizer 似乎没有任何方法来实现这一点。
6个回答

14

Jay说的没错...你需要一个子类。试试这个,它来自我的一个项目。在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];

}

6
不要忘记在DragGestureRecognizer.h文件中添加#import <UIKit/UIGestureRecognizerSubclass.h>。 - HotJard

6

如果您只关心位置信息,无需子类化,您可以从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;
    }
}

1
这个并不获取 UITouch,它只获取特定视图本地框架的 CGPoint - Chris

3

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


而locationOfTouch:inView:也可以分别给出每个触摸的位置。 - Nestor

1
如果您正在编写自己的UIGestureRecognizer,可以通过覆盖获取触摸对象:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

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

Apple文档中有大量关于子类化的信息


0

这里有一种方法可以在任意UIView上添加长按。

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

(据我所发现,您必须使用touchesMoved来进行LongPress,因为touchesBegan在LongPress变为活动状态之前就会触发)

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

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

视图控制器 - .h

                           //nothing special here

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

-2

简单快速:

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

其中 recognizer 是您的 UIGestureRecognizer


3
iOS 8 [<UIPinchGestureRecognizer 0x15eb36d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key touches.'iOS 8中出现的错误信息:[<UIPinchGestureRecognizer 0x15eb36d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key touches.'。意思是,该类不支持键值编码中的“touches”关键字。 - Nicolas Manzini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接