获取UIGestureRecognizer的UITouch对象

有没有办法让
UITouch
对象与手势相关联?
UIGestureRecognizer
似乎没有任何方法。     
已邀请:
杰伊是对的......你会想要一个子类。尝试这个尺寸,它来自我的一个项目。在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];

}
    
如果它只是您感兴趣的位置,则不必进行子类化,您将收到来自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;
    }
}
    
如果您只需要找出手势的位置,可以在UIGestureRecognizer对象上调用locationInView:或locationOfTouch:inView:。但是,如果你想做任何其他事情,那么你需要进行子类化。     
如果您正在编写自己的UIGestureRecognizer,则可以覆盖触摸对象:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
或等效的移动,结束或取消 Apple文档有很多关于子类化的信息     
这是一种将长按添加到任意UIView的方法。 这使您可以在任意数量的UIViews上运行longpress。 在这个例子中,我通过标签限制对UIView方法的访问,但你也可以使用isKindOfClass。 (从我发现你必须使用touchesMoved for 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
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"];
识别器是你的
UIGestureRecognizer
    

要回复问题请先登录注册