Swift 3 中的滑动手势

Posted

技术标签:

【中文标题】Swift 3 中的滑动手势【英文标题】:Swipe gesture in Swift 3 【发布时间】:2016-09-29 07:32:25 【问题描述】:

我试图让 UISwipeGestureRecognizer 在 Swift 3 中工作,默认的向右滑动正常工作,但不能向上或向左。

我已经通过控制拖动一个Action来尝试过

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

而且,在 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)

我的方法

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

现在,除了默认权限外,所有滑动都不起作用。有什么想法吗?

【问题讨论】:

【参考方案1】:

第 1 步:在 viewDidLoad() 方法中添加滑动手势。

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)

第 2 步:检查 handleGesture() 方法中的手势检测

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

我希望这会对某人有所帮助。

更新:

您需要使用@objc 来调用您的“选择器”方法以获取最新的 swift 版本。 即,

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

【讨论】:

您的希望(“我希望这会帮助某人”)已经实现。它对我非常有用。这是一个准确而有效的解释。非常感谢。 Swift 5 let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture)) swipeLeft.direction = .left self.view!.addGestureRecognizer(swipeLeft) @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void if gesture.direction == UISwipeGestureRecognizer.Direction.right print("Swipe Right") 记得使用 @objc 属性向 Objective-C 公开你的 handleGesture() 方法。【参考方案2】:

Swift 4, Xcode 9.2 此代码将使您能够识别用户在整个 ViewController 上的滑动方向(整个 iPhone 屏幕,而不是特定于按钮) 感谢@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"

        
    

【讨论】:

请编辑您的代码并添加一些解释以突出显示使其工作的部分。不鼓励仅使用代码回答。 你应该移除那些手势识别器:***.com/a/26206540/4514671【参考方案3】:

我遇到了同样的问题。实际上,任何滑动都会使我的代码崩溃(来自 Rob Percival,对吗?)。于是我下载了他完成的代码,在 XCode 8 中打开,让它转成 Swift 3。两点改了。

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

在函数内部:

func swiped(_ gesture: UIGestureRecognizer)

【讨论】:

【参考方案4】:

在 Swift 3 中你可以试试这个。

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()

【讨论】:

【参考方案5】:
var swipeGesture = UISwipeGestureRecognizer()

查看并设置 IBOutlet:

@IBOutlet weak var viewSwipe: UIView!

在 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
    

现在,这是在识别滑动手势时调用的方法。

@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% 在我的项目中工作并经过测试

【讨论】:

只需为 Swift 5 更新此代码,将 'UISwipeGestureRecognizerDirection' 更改为 'UISwipeGestureRecognizer.Direction' 即可正常工作。不错的提示老兄!【参考方案6】:
    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")
    
   
  

【讨论】:

【参考方案7】:

我想正在发生的事情是因为您首先添加了右滑动手势识别器,即采用所有手势,而不是将任何手势传递给左滑动手势识别器。

我的建议是查看require(toFail:) 及其similar methods。

这将允许您定义哪些手势识别器需要在其他人接收手势之前失败。例如,您可以告诉左滑动手势识别器它需要右滑动手势识别器才能失败。然后,如果向右滑动手势识别器接收到一个手势但不是向右滑动,它会将手势传递给向左滑动手势识别器。

【讨论】:

以上是关于Swift 3 中的滑动手势的主要内容,如果未能解决你的问题,请参考以下文章

添加手势识别器以允许在 Swift 中的 MapView 上水平滑动

在 Xcode 8.0 Swift 3.0 上检测手势

为啥滑动手势识别器在 swift 中会出错?

在 Swift 中禁用向后滑动手势

Swift - 移动到另一个场景时如何从场景中删除滑动手势?

iOS - 使用/传递手势识别器用于视图中的多个表视图或集合视图(Swift)