For my own convenience, I have created a single UISwipeGestureRecognizer
allowing the user to swipe left and right invoking the same action in code.
I was wondering whether it is possible to detect the actual direction the user swiped inside its action. I found this post点击次数,但可能会使用Swift进行更改。
答案 0 :(得分:0)
重申@LinusGeffarth这是我能提出的最佳解决方案
import UIKit
class SwipeUTIL {
static func addSwipe(selfVC: UIViewController) {
let swipeLeft = UISwipeGestureRecognizer(target: selfVC, action: #selector(selfVC.didSwipe))
let swipeRight = UISwipeGestureRecognizer(target: selfVC, action: #selector(selfVC.didSwipe))
swipeLeft.direction = .left
swipeRight.direction = .right
selfVC.view!.addGestureRecognizer(swipeRight)
selfVC.view!.addGestureRecognizer(swipeLeft)
}
}
extension UIViewController {
internal func didSwipe(swipe: UISwipeGestureRecognizer, completion: (@escaping(UISwipeGestureRecognizerDirection) -> Void)) {
completion(swipe.direction)
}
}
在视图控制器中
override func viewDidLoad() {
super.viewDidLoad()
SwipeUTIL.addSwipe(selfVC: self)
}
override func didSwipe(swipe: UISwipeGestureRecognizer, completion: @escaping ((_ swipedir: UISwipeGestureRecognizerDirection) -> Void)) {
print(swipe.direction)
}