UITapGestureRecognizer在UIScrollView

时间:2016-05-26 21:09:04

标签: ios swift uiviewcontroller uiscrollview uigesturerecognizer

我有UIScrollView启用分页,其中包含两个视图控制器:vc0,vc1。 scrollview包含在UIView(称为" dummyView")中,它包含在自己的视图控制器中。

问题是当包含scrollview的视图控制器设置为初始视图控制器时,vc1不响应触摸。但是,如果将vc1设置为初始视图控制器,则vc1会很好地响应触摸事件

为什么vc1在通过scrollView查看时没有响应触摸?

我已经包含了包含scrollview的视图控制器的viewdidLoad(),我在其中设置了scrollview及其所有子视图。

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var dummyView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.contentSize = CGSizeMake(self.view.frame.width * 2, self.view.frame.height);
    dummyView.frame.size = scrollView.contentSize

    createConstraintsForDummyView()

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("vc0") as! ViewController0

    self.addChildViewController(vc0)
    self.dummyView.addSubview(vc0.view)
    vc0.didMoveToParentViewController(self)

    createConstraintsForVC0(vc0)

    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("vc1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.dummyView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)

    createConstraintsForVC1(vc1)     
}

编辑:我已经包含了vc1的课程

import UIKit

class ViewController1: UIViewController, UIGestureRecognizerDelegate {

func handleTap(recognizer: UITapGestureRecognizer){
    print("Tapped!")
}

override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    tap.delegate = self
    self.view.addGestureRecognizer(tap)

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
}

1 个答案:

答案 0 :(得分:1)

正如所讨论的here

dummyView导致了问题。
解决方案是删除dummyView并改为使用scrollView