简体   繁体   English

如何使UIScrollView中的UIScrollView同时动作

[英]How to make UIScrollView inside a UIScrollView act simultaneously

I'm using a UIScrollView ( UIScrollView_1 ) with paging enabled to move between 3 pages horizontally. 我使用的是UIScrollView( UIScrollView_1 ),启用了分页,可以在3个页面之间水平移动。
On the last page there is multiple UIScrollViews (each one in its own cell) ( UIScrollView_2 ), which when scrolling horizontally to the right (forward) displays the last page (loaded accordingly to which cell that was scrolled). 在最后一页上有多个UIScrollViews(每个UIScrollViews在其自己的单元格中)( UIScrollView_2 ),当向右水平滚动(向前)时,将显示最后一页(相应地加载到要滚动的单元格)。
But if scrolling in the left direction (backwards) I want the UIScrollView_1 to take over and scroll back. 但是,如果向左滚动(向后滚动),我希望UIScrollView_1接管并向后滚动。
This works as intended if I move my finger up and register a new touch-event. 如果我向上移动手指并注册一个新的触摸事件,它将按预期工作。 But I want it to act simultaneously between both the UIScrollViews, on the same pan. 但是我希望它在同一平底锅上的两个UIScrollView之间同时动作。

|-------------UIScrollView_1----------|    
| Page_1 | Page_2 |--UIScrollView_2---|  
..................|...Page_3|Page_4...|

What I want is to make use of the shouldRecognizeSimultaneouslyWith in the UIGestureRecognizerDelegate. 我想要的是利用UIGestureRecognizerDelegate中的shouldRecognizeWithWith。
But when I trying to delegate both the UIScrollViews' to the same ViewController the error 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.' 但是,当我尝试将两个UIScrollViews委派给同一个ViewController时,错误“ UIScrollView的内置平移手势识别器必须将其滚动视图作为其委派。” shows up on the ScrollView_2. 显示在ScrollView_2上。

Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

UPDATE 更新

Here's the cellForRow from page_3's UIViewController that not causing the error with the delegation, but where the UIScrollViews doesn't act simultaneously. 这是page_3的UIViewController中的cellForRow,它不会导致委派错误,但是UIScrollViews不能同时起作用。 (eg need to move the finger up and down to detect the other UIScrollView. (例如,需要上下移动手指以检测其他UIScrollView。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "random", for: indexPath)
    return cell
}

Here's the cellForRow that causing the 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.'-error 这是cellForRow,导致“ UIScrollView的内置平移手势识别器必须以其滚动视图作为其委托。”-错误

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "random", for: indexPath)

    let scrollView_1_VC = self.parent
    cell.scrollView_2.panGestureRecognizer.delegate = scrollView_1_VC

    return cell
}

I solved it. 我解决了
The UIScrollView's built-in UIPanGestureRecognizer must be delegated to it's own UIScrollView as the error 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.' 必须将UIScrollView的内置UIPanGestureRecognizer委托给它自己的UIScrollView,因为错误“ UIScrollView的内置平移手势识别器必须将其滚动视图作为其委托。” said. 说过。
Instead subclass the UIScrollView and create a second UIPanGestureRecognizer which then acts simultaneously with the built-in UIPanGestureRecognizer. 而是子类化UIScrollView并创建第二个UIPanGestureRecognizer,然后与内置的UIPanGestureRecognizer同时操作。

Step 1 - 4: 步骤1-4:

  1. Subclass the cell's UIScrollView and create a second UIPanGestureRecognizer. 子类化单元的UIScrollView并创建第二个UIPanGestureRecognizer。

  2. Add the UIGestureRecognizerDelegate to the CellScrollView and make the shouldRecognizeSimultaneouslyWith return true. 将UIGestureRecognizerDelegate添加到CellScrollView,并使shouldRecognizeSimultaneouslyWith返回true。
    The second UIPanGestureRecognizer will now act simultaneously with the built-in UIPanGestureRecognizer. 现在,第二个UIPanGestureRecognizer将与内置的UIPanGestureRecognizer同时运行。

    class CellScrollView : UIScrollView, UIGestureRecognizerDelegate { 类CellScrollView:UIScrollView,UIGestureRecognizerDelegate {

      var panGesture : UIPanGestureRecognizer! override func awakeFromNib() { panGesture = UIPanGestureRecognizer.init(target: self, action: nil) addGestureRecognizer(panGesture) } func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } 

    } }

  3. In the cellForRow, make the newly created UIPanGestureRecognizer delegate to the UIScrollView_1's UIViewController. 在cellForRow中,将新创建的UIPanGestureRecognizer委托给UIScrollView_1的UIViewController。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {

     if medicines.isEmpty { return tableView.dequeueReusableCell(withIdentifier: "emptyMedCell", for: indexPath) } let cell = tableView.dequeueReusableCell(withIdentifier: "random", for: indexPath) let scrollView_1_VC = self.parent cell.cellScrollView.panGesture.delegate = scrollView_1_VC return cell 

    } }

  4. Lastly, in the ScrollView_1's UIViewController let the shouldRecognizeSimultaneouslyWith return true. 最后,在ScrollView_1的UIViewController中,让shouldRecognizeSimultaneouslyWith返回true。

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

    } }

I think you want to use something like 我想你想使用类似

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

On the UIViewController of ScrollView_2 and have the ScrollView_2's(not the gesture's) delegate as the ViewController, 在ScrollView_2的UIViewController上,并将ScrollView_2(而不是手势的)委托作为ViewController,

ScrollView_2.delegate = self
UIPanGesture.delegate = ScrollView_2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM