2016-04-29 31 views
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 
     } 
    } 
} 
} 

回答

8

我认为你需要实现UIGestureRecognizerDelegate协议

这是否实现你在找什么?

import UIKit 

class ViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate { 

    @IBOutlet weak var scrollView: UIScrollView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // these were made in a storyboard I whipped up for this example. 
     let vc0 = self.storyboard!.instantiateViewControllerWithIdentifier("vc0") 
     let vc1 = self.storyboard!.instantiateViewControllerWithIdentifier("vc1") 

     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 
     // These can be lets because they aren't mutated and I'm using the latest Selector syntax 
     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 

     // ViewController will be the delegate for the left and right swipes 
     swipeRight.delegate = self 
     swipeLeft.delegate = self 

     self.view.addGestureRecognizer(swipeRight) 
     self.view.addGestureRecognizer(swipeLeft) 
     self.view.addGestureRecognizer(swipeUp) 
     self.view.addGestureRecognizer(swipeDown) 
    } 

    // here are those protocol methods with Swift syntax 
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
     return true 
    } 

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { 
     return true 
    } 

    // Debugging - All Swipes Are Detected Now 
    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 
      } 
     } 
    } 
} 
+0

Ah darn!我忘了在我的代码中包含我的变量vc0和vc1。它们放置在全球。现在更新。我尝试了你的代码,而不是你初始化vc0和vc1的方式,它崩溃了。嗯... – theflarenet

+0

你使用的是什么版本的Swift和Xcode?还有什么是崩溃? –

+0

我正在使用最新版本的Swift和xCode。对不起,没有具体。当我开始滑动到vc0时,它崩溃了:'由于未捕获的异常'NSInvalidArgumentException',原因:' - [Project.ViewController respondToSwipeGesture:]:无法识别的选择器发送到实例0x7be75360'' – theflarenet