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. Class AppDelegate.swift: unchanged.
  2. Class ViewController.swift: unchanged.
  3. Class View.swift: subclass of UITextView. Added methods init and swipe(_:).
  4. Main.storyboard
  5. Info.plist

Things to try

  1. Create the four recognizers with an array and a for-in loop. In the init method of class View, change
    		let rightRecognizer: UISwipeGestureRecognizer =
    			UISwipeGestureRecognizer(target: self, action: #selector(View.swipe(_:)));
    		rightRecognizer.direction = UISwipeGestureRecognizerDirection.Right;
    		addGestureRecognizer(rightRecognizer);
    
    		let leftRecognizer: UISwipeGestureRecognizer =
    			UISwipeGestureRecognizer(target: self, action: #selector(View.swipe(_:)));
    		leftRecognizer.direction = UISwipeGestureRecognizerDirection.Left;
    		addGestureRecognizer(leftRecognizer);
    
    		let upRecognizer: UISwipeGestureRecognizer =
    			UISwipeGestureRecognizer(target: self, action: #selector(View.swipe(_:)));
    		upRecognizer.direction = UISwipeGestureRecognizerDirection.Up;
    		addGestureRecognizer(upRecognizer);
    
    		let downRecognizer: UISwipeGestureRecognizer =
    			UISwipeGestureRecognizer(target: self, action: #selector(View.swipe(_:)));
    		downRecognizer.direction = UISwipeGestureRecognizerDirection.Down;
    		addGestureRecognizer(downRecognizer);
    
    to
    		let directions: [UISwipeGestureRecognizerDirection] = [		//an array
    			UISwipeGestureRecognizerDirection.Right,
    			UISwipeGestureRecognizerDirection.Left,
    			UISwipeGestureRecognizerDirection.Up,
    			UISwipeGestureRecognizerDirection.Down
    		];
    
    		for direction in directions {
    			let recognizer: UISwipeGestureRecognizer =
    				UISwipeGestureRecognizer(target: self, action: #selector(View.swipe(_:)));
    
    			recognizer.direction = direction;
    			addGestureRecognizer(recognizer);
    		}
    

  2. Create the four arrow strings with a dictionary. Add the following property to class View.
    	let arrows: [UInt: String] = [		//a dictionary
    		UISwipeGestureRecognizerDirection.Right.rawValue: "→",
    		UISwipeGestureRecognizerDirection.Left.rawValue:  "←",
    		UISwipeGestureRecognizerDirection.Up.rawValue:    "↑",
    		UISwipeGestureRecognizerDirection.Down.rawValue:  "↓"
    	];
    
    In the swipe(_:) method of class View, change
    		let direction: String;
    
    		if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
    			direction = "→";
    		} else if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
    			direction = "←";
    		} else if recognizer.direction == UISwipeGestureRecognizerDirection.Up {
    			direction = "↑";
    		} else if recognizer.direction == UISwipeGestureRecognizerDirection.Down {
    			direction = "↓";
    		} else {
    			direction = "unknown";
    		}
    
    to
    		var direction: String? = arrows[recognizer.direction.rawValue];
    		if direction == nil {
    			direction = "unknown";
    		}
    
    and change the subsequent use of direction to direction!.

  3. We’re printing too many digits:
    start: (37.0, 213.33332824707)
    
    Make it easier to read by converting CGFloat to Float before interpolating it into the string.
    			"start: (\(Float(p.x)), \(Float(p.y))\n" +
    
    start: (37.0, 213.333)
    

  4. Configure a UISwipeGestureRecognizer to call a method of some other object, for example, a method of the view controller. Remove the existing recognizer that recognizes a right swipe. Add the following method to the view controller in ViewController.swift.
    	//This method will be called automatically
    	//when the View's UISwipeGestureRecognizer recognizes a swipe.
    
    	func swipe(recognizer: UISwipeGestureRecognizer) -> Void {
    		if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
    			print("Recognized a right swipe.");
    		} else {
    			print("Recognized a non-right swipe.");
    		}
    	}
    
    Add the following code to the init method of class View.
    		let application: UIApplication = UIApplication.sharedApplication();
    		let appDelegate: AppDelegate = application.delegate as! AppDelegate;
    		let viewController: ViewController = appDelegate.window!.rootViewController as! ViewController;
    
    		let recognizer: UISwipeGestureRecognizer =
    			UISwipeGestureRecognizer(target: viewController, action: #selector(ViewController.swipe(_:)));
    		recognizer.direction = UISwipeGestureRecognizerDirection.Right;
    		addGestureRecognizer(recognizer);