Objective c 获取UIGestureRecognitor的UITouch对象

Objective c 获取UIGestureRecognitor的UITouch对象,objective-c,cocoa-touch,ios,uigesturerecognizer,uitouch,Objective C,Cocoa Touch,Ios,Uigesturerecognizer,Uitouch,有没有办法获取与手势关联的UITouch对象UIGestureRecognizer似乎没有任何方法可用于此操作。如果您正在编写自己的UIGestureRecognizer,则可以让触摸对象覆盖: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 或相应的移动、结束或取消 有很多关于子类化的信息Jay的权利。。。您将需要一个子类。试试这个大小,它来自我的一个项目。在Draggesturererecognizer.h中

有没有办法获取与手势关联的
UITouch
对象
UIGestureRecognizer
似乎没有任何方法可用于此操作。

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
或相应的移动、结束或取消


有很多关于子类化的信息

Jay的权利。。。您将需要一个子类。试试这个大小,它来自我的一个项目。在Draggesturererecognizer.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
当然,您需要实现

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

}

如果只需要找出手势的位置,可以在UIGestureRecognitor对象上调用locationInView:或locationOfTouch:inView:。但是,如果要执行其他操作,则需要对其进行子类化。

如果它只是您感兴趣的位置,则不必对其进行子类化,UIGestureRecognitor会通知您tap的位置更改

使用目标初始化:

self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];
处理UIgestureRecognitzerStateChanged以获取位置更改:

- (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;
    }
}

下面是一种将长按添加到任意UIView的方法

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

(根据我发现的情况,您必须使用TouchsMoved for LongPress,因为TouchsMoved在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
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
                    }
            }
简单快捷:

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

其中识别器是您的
uigestureerecognizer

别忘了添加#导入到Draggesturererecognizer.h文件IOS 8[valueForUndefinedKey:]:此类不符合按键触摸的键值编码。',它只为特定视图的局部帧获取一个
CGPoint
,而locationOfTouch:inView:也分别为您提供每个触摸的位置。
            #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
                    }
            }
NSArray *touches = [recognizer valueForKey:@"touches"];