我相信我找到了一个解决方案,但它是用Obj-C编写的,而我对将其转换为Swift完全陌生和困惑:在UIScrollView子视图中的水平UISwipeGestureRecognizer?(UIScrollView仅需要识别垂直滚动)
我有一个Main.storyboard,在其中添加了两个子视图,可以在它们之间水平滚动。目前,上下滑动被检测到,但由于UIScrollView,左右滑动没有被检测到。有什么解决此干扰的方法吗?
Main.storyboard:
// Global
let vc0 = ViewController0(nibName: "ViewController0", bundle: nil)
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
class ViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController(vc0)
self.scrollView.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)
var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame = frame1
self.addChildViewController(vc1)
self.scrollView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 66)
self.scrollView.delegate = self
// Swipe Gesture Recognizers
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
swipeRight.delegate = self
swipeLeft.delegate = self
self.view.addGestureRecognizer(swipeRight)
self.view.addGestureRecognizer(swipeLeft)
self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return true
}
// Debugging - Only Up & Down Swipes Are Detected
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
}
}
}
}
Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '-[Project.ViewController respondToSwipeGesture:]:无法识别发送到实例0x7be75360的选择器'
。 - theflarenetrespondToSwipeGesture
不在viewController
的范围内。你需要它在外面吗?在我的例子中,我把respondToSwipeGesture
设为了viewController
的一个方法。 - Andrew Sowers