Search code examples
iosuigesturerecognizeruiswipegesturerecognizer

No visible interface for UISwipeGestureRecognizer declares the selector 'touchesMoved:withEvent:'


I got an error. "No visible interface for UISwipeGestureRecognizer declares the selector 'touchesMoved:withEvent:'"

I looked at documentation and found touchesMoved:withEvent at UIGestureRecognizer class. How do I solve this error?

@interface MySwipeRecognizer : UISwipeGestureRecognizer

@implementation MySwipeRecognizer

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 [super touchesMoved:touches withEvent:event];
}
@end

Solution

  • Unless I'm misunderstanding the question, a UISwipeGestureRecognizer does all the touch handling for you. Your code would look something like this:

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
    
    // set a direction for the swipe
    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    // self is a view to add the recognizer to:
    [self addGestureRecognizer:swipe];
    
    .
    .
    .
    
    - (void) onSwipe:(id)sender
    {
     // a swipe has been recognized!
    }
    

    UIGestureRecognizer is an ABSTRACT class, so concrete implementations like UISwipeGestureRecognizer do all the touch event handling for you. If you're trying to create your own custom gesture recognizer, you'd subclass UIGestureRecognizer.