繁体   English   中英

Swift 3 中的滑动手势

[英]Swipe gesture in Swift 3

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

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

@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:在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) -> Void {
   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 版本调用您的“选择器”方法。 IE,

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

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"

        }
    }
}

建造

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

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

在函数内部:

func swiped(_ gesture: UIGestureRecognizer)

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

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

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

我的建议是查看require(toFail:)及其类似的方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM