2017-07-07 57 views
0

我有下面的代碼,設置了一個垂直scrollview我的問題是視圖控制器內的視圖沒有準確地響應滑動手勢。例如,在此滾動視圖中,bottomVc包含左右滑動的輕掃手勢。當這個視圖控制器在另一個項目中被隔離時,它完美地工作,但是當它在下面的滾動視圖內時,滑動手勢不起作用。我想知道如何解決這個問題,以便我的viewcontollers可以在滾動視圖中處理滑動手勢。此外,僅供參考,控制其他視圖控制器的代碼與滑動手勢一起位於單獨的文件中。 代碼:
進口的UIKitUIScrollView正在吃我的輕掃手勢Swift

class VerticalScrollViewController: UIViewController, SnapContainerViewControllerDelegate { 

var topVc: UIViewController! 
var middleVc: UIViewController! 
var bottomVc: UIViewController! 
var scrollView: UIScrollView! 

class func verticalScrollVcWith(middleVc: UIViewController, 
           topVc: UIViewController?=nil, 
           bottomVc: UIViewController?=nil) -> VerticalScrollViewController { 
    let middleScrollVc = VerticalScrollViewController() 

    middleScrollVc.topVc = topVc 
    middleScrollVc.middleVc = middleVc 
    middleScrollVc.bottomVc = bottomVc 

    return middleScrollVc 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view: 
    setupScrollView() 
} 

func setupScrollView() { 
    scrollView = UIScrollView() 
    scrollView.isPagingEnabled = true 
    scrollView.showsVerticalScrollIndicator = false 
    scrollView.bounces = false 



    let view = (
     x: self.view.bounds.origin.x, 
     y: self.view.bounds.origin.y, 
     width: self.view.bounds.width, 
     height: self.view.bounds.height 
    ) 

    scrollView.frame = CGRect(x: view.x, y: view.y, width: view.width, height: view.height) 
    self.view.addSubview(scrollView) 

    let scrollWidth: CGFloat = view.width 
    var scrollHeight: CGFloat 

    if topVc != nil && bottomVc != nil { 
     scrollHeight = 3 * view.height 
     middleVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height) 
     topVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height) 
     bottomVc.view.frame = CGRect(x: 0, y: 2 * view.height, width: view.width, height: view.height) 

     addChildViewController(topVc) 
     addChildViewController(middleVc) 
     addChildViewController(bottomVc) 

     scrollView.addSubview(topVc.view) 
     scrollView.addSubview(middleVc.view) 
     scrollView.addSubview(bottomVc.view) 

     topVc.didMove(toParentViewController: self) 
     middleVc.didMove(toParentViewController: self) 
     bottomVc.didMove(toParentViewController: self) 

     //scrollView.contentOffset.y = middleVc.view.frame.origin.y 
     scrollView.contentOffset.y = topVc.view.frame.origin.y 

    } else if topVc == nil { 
     scrollHeight = 2 * view.height 
     middleVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height) 
     bottomVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height) 

     addChildViewController(middleVc) 
     addChildViewController(bottomVc) 

     scrollView.addSubview(middleVc.view) 
     scrollView.addSubview(bottomVc.view) 

     middleVc.didMove(toParentViewController: self) 
     bottomVc.didMove(toParentViewController: self) 

     scrollView.contentOffset.y = 0 

    } else if bottomVc == nil { 
     scrollHeight = 2 * view.height 
     topVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height) 
     middleVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height) 

     addChildViewController(topVc) 
     addChildViewController(middleVc) 

     scrollView.addSubview(topVc.view) 
     scrollView.addSubview(middleVc.view) 

     topVc.didMove(toParentViewController: self) 
     middleVc.didMove(toParentViewController: self) 

     scrollView.contentOffset.y = middleVc.view.frame.origin.y 

    } else { 
     scrollHeight = view.height 
     middleVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height) 

     addChildViewController(middleVc) 
     scrollView.addSubview(middleVc.view) 
     middleVc.didMove(toParentViewController: self) 
    } 

    scrollView.contentSize = CGSize(width: scrollWidth, height: scrollHeight) 
    scrollView.delaysContentTouches = false 

} 


func outerScrollViewShouldScroll() -> Bool { 
    if scrollView.contentOffset.y < middleVc.view.frame.origin.y || scrollView.contentOffset.y > middleVc.view.frame.origin.y { 

     return false 

    } else { 

     return true 
    } 

} 

} 
+0

'UIGestureRecognizerDelegate'應該有你需要的方法。具體如下:https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer – PeejWeej

回答

1

嘗試添加UIGestureRecognizerDelegate協議上您的課,再加入此方法返回true

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

應該工作,除非你的控制器設置以某種方式阻礙正常touch recognition ..

+0

你擊敗了我! – Mozahler

+0

在scrollview類或單個視圖控制器類? – JustANoob

+0

在包含滾動視圖的類中,理想情況下,在任何包含觸摸密集型(拖動或長按)或可滾動元素本身(集合,表格等)的子視圖中, – murphguy