获取UIGestureRecognizer的UITouch对象

Mor*_*ess 14 cocoa-touch objective-c uitouch uigesturerecognizer ios

有没有办法让UITouch对象与手势相关联?UIGestureRecognizer似乎没有任何方法.

wpe*_*rse 13

杰伊是对的......你会想要一个子类.尝试这个尺寸,它来自我的一个项目.在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
Run Code Online (Sandbox Code Playgroud)

在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 
Run Code Online (Sandbox Code Playgroud)

当然,你需要实现

- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event; 
Run Code Online (Sandbox Code Playgroud)

您的委托中的方法 - 例如:

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

}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记将#import <UIKit/UIGestureRecognizerSubclass.h>添加到DragGestureRecognizer.h文件中 (6认同)

pfo*_*pfo 6

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

使用目标初始化:

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

处理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;
    }
}
Run Code Online (Sandbox Code Playgroud)