Swipe gesture in Swift 3

后端 未结 7 1212
感动是毒
感动是毒 2020-12-07 17:33

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 d

相关标签:
7条回答
  • 2020-12-07 18:03

    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()
    }
    
    0 讨论(0)
  • 2020-12-07 18:04

    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"
    
            }
        }
    }
    

    0 讨论(0)
  • 2020-12-07 18:05

    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)
    
    0 讨论(0)
  • 2020-12-07 18:22
    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

    0 讨论(0)
  • 2020-12-07 18:23
        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")
        }
       }
      }
    
    0 讨论(0)
  • 2020-12-07 18:25

    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.

    0 讨论(0)
提交回复
热议问题