Class UISwipeGestureRecognizer

Class UISwipeGestureRecognizer is a subclass of UIGestureRecognizer. It gives you no control over how long or fast or straight the swipe is required to be. But that’s good: if every app used this recognizer, they would all have the same requirements.

I don’t see any claim in the documentation that a single UISwipeGestureRecognizer can recognize both a left swipe or a right swipe, so I made a separate recognizer for each direction. A swipe consists of just one touch, but there may be gestures that consist of a larger numberOfTouches.

Source code in Swipe.zip

  1. main.m
  2. Class SwipeAppDelegate
  3. Class View

Things to try

  1. Create the four recognizers with a loop. In the initWithFrame: method of class View, change
    		UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]
    			initWithTarget: self action: @selector(swipe:)
    		];
    		recognizer.direction = UISwipeGestureRecognizerDirectionRight;
    		[self addGestureRecognizer: recognizer];
    
    		recognizer = [[UISwipeGestureRecognizer alloc]
    			initWithTarget: self action: @selector(swipe:)
    		];
    		recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    		[self addGestureRecognizer: recognizer];
    
    		recognizer = [[UISwipeGestureRecognizer alloc]
    			initWithTarget: self action: @selector(swipe:)
    		];
    		recognizer.direction = UISwipeGestureRecognizerDirectionUp;
    		[self addGestureRecognizer: recognizer];
    
    		recognizer = [[UISwipeGestureRecognizer alloc]
    			initWithTarget: self action: @selector(swipe:)
    		];
    		recognizer.direction = UISwipeGestureRecognizerDirectionDown;
    		[self addGestureRecognizer: recognizer];
    
    to
    		static const UISwipeGestureRecognizerDirection a[] = {
    			UISwipeGestureRecognizerDirectionRight,
    			UISwipeGestureRecognizerDirectionLeft,
    			UISwipeGestureRecognizerDirectionUp,
    			UISwipeGestureRecognizerDirectionDown
    		};
    		static const size_t n = sizeof a / sizeof a[0];
    
    		for (size_t i = 0; i < n; ++i) {
    			UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]
    				initWithTarget: self action: @selector(swipe:)
    			];
    
    			recognizer.direction = a[i];
    			[self addGestureRecognizer: recognizer];
    		}
    

  2. Create the four arrows with a dictionary. Add the following instance variable to class View.
    	NSDictionary *arrows;
    
    Initialize the dictionary in the initWithFrame: method of class View before you create the gesture recognizers. The @() is an Objective-C literal that creates an NSNumber object.
    	arrows = [NSDictionary dictionaryWithObjectsAndKeys:
    		@"→", @(UISwipeGestureRecognizerDirectionRight),
    		@"←", @(UISwipeGestureRecognizerDirectionLeft),
    		@"↑", @(UISwipeGestureRecognizerDirectionUp),
    		@"↓", @(UISwipeGestureRecognizerDirectionDown),
    		nil
    	];
    
    In the swipe: method of class View, change
    	NSString *direction = @"unknown";
    	if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
    		direction = @"→";
    	} else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
    		direction = @"←
    	} else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
    		direction = @"↑";
    	} else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
    		direction = @"↓";
    	}
    
    to
    	NSString *direction = [arrows objectForKey:
    		[NSNumber numberWithInt: recognizer.direction]
    	];
    
    	if (direction == nil) {
    		direction = @"unknown";
    	}