2010-12-15 27 views

Trả lời

13

Quyền của Jay ... bạn sẽ muốn có một phân lớp. Hãy thử cái này cho kích thước, nó là từ một trong những dự án của tôi. Trong 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 

Và trong 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 

Tất nhiên, bạn sẽ cần phải thực hiện các phương pháp

- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event; 

trong đại biểu của bạn - ví dụ :

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

Đừng quên thêm #import để DragGestureRecognizer.h nộp – HotJard

1

Nếu bạn đang viết UIGestureRecognizer riêng của bạn, bạn có thể nhận được các đối tượng liên lạc trọng:

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

hoặc tương đương di chuyển, kết thúc hoặc hủy

Các Apple docs có rất nhiều thông tin về subclassing

2

Nếu bạn chỉ cần tìm ra vị trí của cử chỉ, bạn có thể gọi vị tríInView: hoặc locationOfTouch: inView: trên đối tượng UIGestureRecognizer. Tuy nhiên nếu bạn muốn làm bất cứ điều gì khác, sau đó bạn sẽ cần phải phân lớp.

6

Nếu đó chỉ là vị trí bạn quan tâm, bạn không phải phân lớp, bạn sẽ được thông báo về những thay đổi về vị trí của vòi từ trình nhận dạng UIGestureRecognizer.

Initialize với mục tiêu:

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

Xử lý UIGestureRecognizerStateChanged để có được những thay đổi vị trí:

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

này doesn' t nhận được 'UITouch', nó chỉ nhận được một' CGPoint' cho khung cục bộ của khung nhìn cụ thể. – Chris

0

Dưới đây là một phương pháp để có được một nền báo chí dài thêm vào một UIView tùy ý.

Điều này cho phép bạn chạy longpress trên bất kỳ số lượng UIView nào. Trong ví dụ này, tôi hạn chế quyền truy cập vào phương thức UIView thông qua các thẻ nhưng bạn cũng có thể sử dụng isKindOfClass.

(Từ những gì tôi tìm thấy bạn phải sử dụng touchesMoved cho nhấn và giữ vì touchesBegan cháy trước khi nhấn và giữ sẽ được kích hoạt)

lớp con - .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 

lớp con - .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 
        } 
      } 
-1

đơn giản và nhanh chóng:

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

đâu recognizer là bạn UIGestureRecognizer

+2

iOS 8 [ valueForUndefinedKey:]: lớp này không phù hợp với mã hóa khóa cho các lần chạm phím. ' –

Các vấn đề liên quan