2014-03-06 3 views
0

У меня вопрос .. Я хотел бы сделать свой собственный распознаватель жестов с помощью салфетки и что вы перетаскиваете (или прокручиваете) вниз двумя пальцами, но я не знаю, как это сделать.Как сделать собственный распознаватель жестов?

Это мой код GestureSwipe.h:

#import <UIKit/UIKit.h> 

@interface GestureSwipe : UIGestureRecognizer 

@property CGPoint startTouchPosition; 

@property(nonatomic) NSUInteger numberOfTouchesRequired; 
@property(nonatomic) UISwipeGestureRecognizerDirection direction; 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

@end 

Это мой код GestureSwipe.m:

#define VERT_SWIPE_DRAG_MAX 7 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *aTouch = [touches anyObject]; 
    // startTouchPosition is a property 
    self.startTouchPosition = [aTouch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *aTouch = [touches anyObject]; 
    CGPoint currentTouchPosition = [aTouch locationInView:self.view]; 
    // Check if direction of touch is horizontal and long enough 
    if (fabsf(self.startTouchPosition.y - currentTouchPosition.y) <= 
     VERT_SWIPE_DRAG_MAX) 
    { 
     // If touch appears to be a swipe 
     if (self.startTouchPosition.y < currentTouchPosition.y) { 

      [self myProcessDownSwipe:touches withEvent:event]; 
     } 
     self.startTouchPosition = CGPointZero; 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 

     self.startTouchPosition = CGPointZero; 
} 

-(void)myProcessDownSwipe:(NSSet *)touches withEvent:(UIEvent *)event { 

} 

Как сделать прокрутку двумя пальцами?

Тогда у меня есть другие VC, где признают свой собственный жест:

HelloViewController.m

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    //newGesture = [[GestureSwipe alloc] init]; 

    newGesture = [[GestureSwipe alloc] initWithTarget:self action:@selector(showImage:)]; 

    newGesture.numberOfTouchesRequired = 2; 


} 
+0

Вы можете взглянуть на документацию UISwipeGestureRecognizer, вы можете установить количество штрихов, необходимых (так, 2 в данном случае) и направление (вниз) https://developer.apple.com/library/ios/documentation/uikit/reference/UISwipeGestureRecognizer_Class/Reference/Reference.html –

+0

Я видел, что и я использую эти свойства, но я не знаю, где мне нужно использовать эти свойства: '@property (неатомический) номер NSUIntegerOfTouchesRequired; @property (nonatomic) UISwipeGestureRecognizer направление направления; ' – user1911

+0

Вы использовали бы его в самом распознавателе жестов, я добавлю больше деталей в ответ, чтобы включить форматированный код. –

ответ

0

В HelloViewController вы бы добавить что-то вроде этого:

UISwipeGestureRecognizer *recognizer; 

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 

// Set the acceptable swipe direction and number of touches required to do the gesture 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
recognizer.numberOfTouchesRequired = 2; 

// Add the gesture recogizer to the view 
[self.view addGestureRecognizer:recognizer]; 

Тогда вы создаст настраиваемый метод (я назвал свой one handleSwipe :, но это может быть любым, что вам подходит) и делать то, что вам нужно t o делаю.

EDIT: Так что ваш метод viewDidLoad будет выглядеть следующим образом

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    UISwipeGestureRecognizer *recognizer; 

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 

    // Set the acceptable swipe direction and number of touches required to do the gesture 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
    recognizer.numberOfTouchesRequired = 2; 

    // Add the gesture recogizer to the view 
    [self.view addGestureRecognizer:recognizer]; 


} 

И в handleSwipe: метод может выглядеть так:

-(void)handleSwipe:(id)sender{ 

    // Do something 

} 
+0

Почему? Мне нужно сделать собственный распознаватель жестов из моего класса GestureSwipe.h no UISWipeGestureRecognizer – user1911

+0

Что делает ваш собственный класс, который отличается от стандартного распознавателя жестов? Нет смысла изобретать колесо, и даже если вам нужен ваш собственный класс, вы действительно должны быть его подклассом UISwipeGestureRecognizer, если я не понял цель вашего класса. –

0

Там нет необходимости, чтобы создать свой собственный распознаватель. Вы можете справиться со стандартным распознавателем и просто указать, сколько требуется коснуться.

Хороший пример можно найти здесь: Capture only UIView 2 finger UIPanGestureRecognizer

Смежные вопросы