swift – 如何识别所有4个方向的滑动

swift – 如何识别所有4个方向的滑动,第1张

概述我需要使用滑动来识别滑动手势,然后向右。但在swift UISwipeGestureRecognizer具有预定的右方向..我不知道如何使这个使用其他方向.. 您需要为每个方向有一个UISwipeGestureRecognizer。这有点怪异,因为UISwipeGestureRecognizer.direction属性是一个选项样式的位掩码,但每个识别器只能处理一个方向。你可以将它们全部发送到同一 我需要使用滑动来识别滑动手势,然后向右。但在swift UISwipeGestureRecognizer具有预定的右方向..我不知道如何使这个使用其他方向.. 您需要为每个方向有一个UISwipeGestureRecognizer。这有点怪异,因为UISwipeGestureRecognizer.direction属性是一个选项样式的位掩码,但每个识别器只能处理一个方向。你可以将它们全部发送到同一个处理程序,如果你想,并将其排序,或发送到不同的处理程序。这里有一个实现:
overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    var swipeRight = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")    swipeRight.direction = UISwipeGestureRecognizerDirection.Right    self.vIEw.addGestureRecognizer(swipeRight)    var swipeDown = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")    swipeDown.direction = UISwipeGestureRecognizerDirection.Down    self.vIEw.addGestureRecognizer(swipeDown)}func respondToSwipeGesture(gesture: UIGestureRecognizer) {    if let swipeGesture = gesture as? UISwipeGestureRecognizer {        switch swipeGesture.direction {            case UISwipeGestureRecognizerDirection.Right:                print("Swiped right")            case UISwipeGestureRecognizerDirection.Down:                print("Swiped down")            case UISwipeGestureRecognizerDirection.left:                print("Swiped left")            case UISwipeGestureRecognizerDirection.Up:                print("Swiped up")            default:                break        }    }}

Swift 3:

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let swipeRight = UISwipeGestureRecognizer(target: self,action: #selector(self.respondToSwipeGesture))    swipeRight.direction = UISwipeGestureRecognizerDirection.right    self.vIEw.addGestureRecognizer(swipeRight)    let swipeDown = UISwipeGestureRecognizer(target: self,action: #selector(self.respondToSwipeGesture))    swipeDown.direction = UISwipeGestureRecognizerDirection.down    self.vIEw.addGestureRecognizer(swipeDown)}func respondToSwipeGesture(gesture: UIGestureRecognizer) {    if let swipeGesture = gesture as? UISwipeGestureRecognizer {        switch swipeGesture.direction {        case UISwipeGestureRecognizerDirection.right:            print("Swiped right")        case UISwipeGestureRecognizerDirection.down:            print("Swiped down")        case UISwipeGestureRecognizerDirection.left:            print("Swiped left")        case UISwipeGestureRecognizerDirection.up:            print("Swiped up")        default:            break        }    }}
总结

以上是内存溢出为你收集整理的swift – 如何识别所有4个方向的滑动全部内容,希望文章能够帮你解决swift – 如何识别所有4个方向的滑动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存