在Swift 3中滑动手势

时间:2016-09-29 07:32:25

标签: swift

我试图让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")
        }
    }
}

现在,除默认权限外,没有任何滑动工作正在进行。有什么想法吗?

7 个答案:

答案 0 :(得分:77)

第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 == UISwipeGestureRecognizerDirection.right {
        print("Swipe Right")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.left {
        print("Swipe Left")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.up {
        print("Swipe Up")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.down {
        print("Swipe Down")
   }
}

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

答案 1 :(得分:15)

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"

        }
    }
}

Build

答案 2 :(得分:14)

我遇到了同样的问题。实际上,任何刷卡都会使我的代码崩溃(来自Rob Percival,对吧?)。所以我下载了他完成的代码,在XCode 8中打开它并让它转换为Swift 3.两点被改变了。

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

在函数内部:

func swiped(_ gesture: UIGestureRecognizer)

答案 3 :(得分:3)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

我想象发生的事情是因为您首先要添加正确的滑动手势识别器,即采取所有手势而不是将任何手势传递到左侧滑动手势识别器。

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

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

答案 6 :(得分:0)

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