2010-12-15 56 views

回答

13
取消

Apple docs有大量的信息

傑伊的權利...你會想要一個子類。試試這個大小,它來自我的一個項目。在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文件 – HotJard 2013-02-17 14:21:22

1

如果你正在寫自己的UIGestureRecognizer你可以觸摸的物體覆蓋:

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

或等效的移動,結束或繼承

2

如果您只需要查找手勢的位置,則可以調用UIGestureRecognizer對象上的locationInView:或locationOfTouch:inView:。但是如果你想做其他事情,那麼你需要繼承子類。

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

這沒有按」 t獲得'UITouch',它只爲特定視圖的本地幀獲取'CGPoint'。 – Chris 2014-12-14 19:58:27

0

這裏得到一個長按添加到任意的UIView的方法。

這可讓您在任意數量的UIView上運行longpress。 在這個例子中,我通過標籤限制對UIView方法的訪問,但也可以使用isKindOfClass。

(從我發現你必須使用touchesMoved爲長按,因爲火災的touchesBegan長按該變活動前)

子類 - .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 - 。米

  #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 
        } 
      } 
-1

簡單快捷:

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

識別在哪裏你UIGestureRecognizer

+2

iOS 8 [ valueForUndefinedKey:]:該類不是關鍵值觸發的關鍵字編碼標準。 – 2014-11-10 10:48:46