【问题标题】:Getting the UITouch objects for a UIGestureRecognizer获取 UIGestureRecognizer 的 UITouch 对象
【发布时间】:2010-12-15 04:56:59
【问题描述】:

有没有办法获取与手势关联的UITouch 对象? UIGestureRecognizer 似乎没有任何方法。

【问题讨论】:

    标签: objective-c cocoa-touch ios uigesturerecognizer uitouch


    【解决方案1】:

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

    【讨论】:

    • 别忘了在 DragGestureRecognizer.h 文件中添加#import
    【解决方案2】:

    如果只是你感兴趣的位置,则不必子类化,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;
        }
    }
    

    【讨论】:

    • 这没有得到UITouch,它只得到一个特定视图的本地框架的CGPoint
    【解决方案3】:

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

    【讨论】:

    • 而 locationOfTouch:inView: 还分别为您提供每次触摸的位置。
    【解决方案4】:

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

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

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

    Apple docs 有很多关于子类化的信息

    【讨论】:

      【解决方案5】:

      这是一种将长按添加到任意 UIView 的方法。

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

      (根据我的发现,您必须为 LongPress 使用 touchesMoved,因为 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
                          }
                  }
      

      【讨论】:

        【解决方案6】:

        简单快速:

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

        识别器是您的UIGestureRecognizer

        【讨论】:

        • iOS 8 [ valueForUndefinedKey:]:这个类不符合键值编码。'
        猜你喜欢
        • 1970-01-01
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多