I need to use swipe to recognize swipe gesture down and then right. But on swift UISwipeGestureRecognizer has predeterminate Right direction.. And I don't know how make this for use other directions..

推荐答案

You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
    swipeDown.direction = .down
    self.view.addGestureRecognizer(swipeDown)
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case .right:
            print("Swiped right")
        case .down:
            print("Swiped down")
        case .left:
            print("Swiped left")
        case .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
        }
    }
}

Ios相关问答推荐

从后台线程调用DispatchObject的方法安全吗?

如果没有等待,SWIFT Async让任务抛出取消异常

当文本为空时,具有垂直轴的SwiftUI文本字段不会与FirstTextBaseline对齐

视图未更新以在按下按钮时显示另一个视图

AVAudioRecord 没有音频

如何以编程方式触发 SwiftUI DatePicker?

从 PHAssets 生成图像时,iOS 应用程序因内存问题而崩溃

在 SwiftUI 中重构提取到子视图的 GeometryReader 代码

如何根据条件快速将返回类型设为 LinearGradient 或 AngularGradient?

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

卡在 Apple 的 SwiftUI 教程第 5 章(更新应用数据)

Swiftui为什么在点击第二行之前背景不起作用

来自 c++ 的runtime_error未在 iOS 中捕获

`Task` 在内部调用异步函数时阻塞主线程

如何将标准化坐标系中的点的位置转换为具有相对位置的常规坐标系?

Swift 错误:在其自己的初始值中使用的变量

如何在导航栏右侧添加多个 UIBarButtonItem?

从 NSUserDefaults 字典 iOS 中删除所有键

iOS UITextView 或 UILabel 带有可点击的动作链接

为什么 Xcode 4 不创建任何产品?