create horizontal swipe gesture in swift

Here's a code snippet that demonstrates how to create a horizontal swipe gesture recognizer in Swift using UIKit:

main.swift
// Create a variable to hold the swipe gesture recognizer
var swipeGestureRecognizer: UISwipeGestureRecognizer!

// Instantiate the gesture recognizer with a `horizontal` direction and add it to your view
swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleHorizontalSwipe))
swipeGestureRecognizer.direction = .horizontal
view.addGestureRecognizer(swipeGestureRecognizer)

// Define the action that should be taken when the gesture is recognized
@objc func handleHorizontalSwipe(_ sender: UISwipeGestureRecognizer) {
    print("Horizontal swipe detected")
    // Add your own custom code here
}
631 chars
14 lines

You can configure the direction property of the gesture recognizer to be .left or .right depending on which direction you want your horizontal swipe to be recognized in. You can also change the action that is taken when the gesture is recognized to fit your application's needs.

gistlibby LogSnag