user1908962
user1908962

Reputation: 543

Swipe gesture in Swift 3

Im trying to get a UISwipeGestureRecognizer to work in Swift 3, the default swipe right is working correctly though not up down or left.

I have tried it by control dragging an Action

@IBAction func hole(_ recognizer: UISwipeGestureRecognizer) {
    if (recognizer.direction == UISwipeGestureRecognizerDirection.left)
    {
        print("left")
    }else if recognizer.direction == .right {
        print("right")
    }else {
        print("other")
    }
}

And, in ViewDidLoad

//gesture recognisers
let swipeRight = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)

to my method

func holeSwiped(gesture: UISwipeGestureRecognizer)
{
    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("right swipe")
        case UISwipeGestureRecognizerDirection.left:
            print("left swipe")
        default:
            print("other swipe")
        }
    }
}

Now, none of the swipes are working except the default right. Any ideas?

Upvotes: 53

Views: 63940

Answers (7)

Ram Madhavan
Ram Madhavan

Reputation: 2388

Step 1: Add swipe Gesture(s) in viewDidLoad() method.

override func viewDidLoad() {
    super.viewDidLoad()
        
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)
        
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)
        
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeUp.direction = .up
    self.view.addGestureRecognizer(swipeUp)
        
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeDown.direction = .down
    self.view.addGestureRecognizer(swipeDown)
}

Step 2: Check the gesture detection in handleGesture() method

func handleGesture(gesture: UISwipeGestureRecognizer) {
   if gesture.direction == .right {
        print("Swipe Right")
   }
   else if gesture.direction == .left {
        print("Swipe Left")
   }
   else if gesture.direction == .up {
        print("Swipe Up")
   }
   else if gesture.direction == .down {
        print("Swipe Down")
   }
}

I hope this will help someone.

UPDATE:

You need to use @objc to call your 'Selector' method for latest swift versions. ie,

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
  .
  .
  .
}

Upvotes: 114

mikeT
mikeT

Reputation: 363

Swift 4, Xcode 9.2 This code will enable you to Recognize the Swipe Direction of the user on the whole ViewController (All of the iPhone screen, not specific to a button) Thanks @Ram-madhavan

import UIKit

class ViewController: UIViewController {

    //Label to show test and see Gesture direction. 

    @IBOutlet weak var swipeDirectionLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
      //Defining the Various Swipe directions (left, right, up, down)  
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeUp.direction = .up
        self.view.addGestureRecognizer(swipeUp)

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeDown.direction = .down
        self.view.addGestureRecognizer(swipeDown) 
    }

    //Function to Print to console Swipe Direction, and Change the label to show the directions. The @objc before func is a must, since we are using #selector (above). You can add to the function, in my case, I'll add a sound, so when someone flips the page, it plays a page sound. 

    @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == UISwipeGestureRecognizerDirection.right {
            print("Swipe Right")
            swipeDirectionLabel.text = "Swiped Right"
        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.left {
            print("Swipe Left")
            swipeDirectionLabel.text = "Swiped Left"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.up {
            print("Swipe Up")
            swipeDirectionLabel.text = "Swiped Up"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.down {
            print("Swipe Down")
            swipeDirectionLabel.text = "Swiped Down"

        }
    }
}

Build

Upvotes: 20

Mr.Javed Multani
Mr.Javed Multani

Reputation: 13294

var swipeGesture = UISwipeGestureRecognizer()

Take view and set IBOutlet:

@IBOutlet weak var viewSwipe: UIView!

Write this pretty code on viewDidLoad()

let direction: [UISwipeGestureRecognizerDirection] = [.up, .down, .left, .right]
    for dir in direction{
        swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeView(_:)))
        viewSwipe.addGestureRecognizer(swipeGesture)
        swipeGesture.direction = dir
        viewSwipe.isUserInteractionEnabled = true
        viewSwipe.isMultipleTouchEnabled = true
    }

Now, this is method is calling when swipe gesture is recognized.

@objc func swipeView(_ sender:UISwipeGestureRecognizer){
    UIView.animate(withDuration: 1.0) {
        if sender.direction == .right{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .left{
            self.viewSwipe.frame = CGRect(x: 0, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .up{
             self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: 0, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .down{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.view.frame.size.height - self.viewSwipe.frame.size.height, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }
    }
}

100% working in my project and tested

Upvotes: 2

Matthew Hallatt
Matthew Hallatt

Reputation: 1390

I imagine what's happening is because you're adding your right swipe gesture recogniser first, that is taking all gestures and not passing any on to the left swipe gesture recogniser.

My suggestion would be to look at require(toFail:) and its similar methods.

This would allow you to define which gesture recognisers need to fail before others receive gestures. For example, you could tell your left swipe gesture recogniser that it requires the right swipe gesture recogniser to fail. Then if the right swipe gesture recogniser receives a gesture but it isn't a right swipe, it will pass the gesture on to the left swipe gesture recogniser.

Upvotes: 0

Berlin
Berlin

Reputation: 2201

In, Swift 3 you can try this.

let swipeRightOrange = UISwipeGestureRecognizer(target: self, action:#selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = UISwipeGestureRecognizerDirection.Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = UISwipeGestureRecognizerDirection.Left;

@IBAction func  slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blueColor()
}
@IBAction func slideToRightWithGestureRecognizer
(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGrayColor()
}

Upvotes: 4

Sai kumar Reddy
Sai kumar Reddy

Reputation: 1829

    func holeSwiped(gesture: UISwipeGestureRecognizer)
    {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
    switch swipeGesture.direction {
    case UISwipeGestureRecognizerDirection.right:
        NSLog("right swipe")
    case UISwipeGestureRecognizerDirection.left:
        NSLog("left swipe")
    default:
        NSLog("other swipe")
    }
   }
  }

Upvotes: 0

paulomatsui
paulomatsui

Reputation: 151

I was having the same problem. Actually, any swipe just crashed my code (from Rob Percival, right?). So I downloaded his finished code, opened it in XCode 8 and let it convert to Swift 3. Two points were changed.

In ViewDidLoad:
  let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))

And inside the function:

func swiped(_ gesture: UIGestureRecognizer)

Upvotes: 15

Related Questions