4
我相信我找到了一個解決方案,但它在對象 - 和我完全新&混淆成解釋到這一點斯威夫特:Horizontal UISwipeGestureRecognizer in subview of UIScrollView ? (UIScrollView only needs to recognize vertical scrolling)斯威夫特 - 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
}
}
}
}
Ah darn!我忘了在我的代碼中包含我的變量vc0和vc1。它們放置在全球。現在更新。我嘗試了你的代碼,而不是你初始化vc0和vc1的方式,它崩潰了。嗯... – theflarenet
你使用的是什麼版本的Swift和Xcode?還有什麼是崩潰? –
我正在使用最新版本的Swift和xCode。對不起,沒有具體。當我開始滑動到vc0時,它崩潰了:'由於未捕獲的異常'NSInvalidArgumentException',原因:' - [Project.ViewController respondToSwipeGesture:]:無法識別的選擇器發送到實例0x7be75360'' – theflarenet