我正在使用启用了分页功能的UIScrollView(UIScrollView_1)在3个页面之间水平移动。
在最后一页上有多个UIScrollViews(每个视图都在自己的单元格中)(UIScrollView_2),当水平向右滚动(向前)时,它们会显示最后一页(相应地加载到滚动的单元格)。
但是,如果向左滚动(向后),我希望UIScrollView_1接管并向后滚动。
如果我向上移动手指并注册新的触摸事件,这将按预期工作。但我希望它在同一平底锅上同时在两个 UIScrollView 之间起作用。
|-------------UIScrollView_1----------|
| Page_1 | Page_2 |--UIScrollView_2---|
..................|...Page_3|Page_4...|
我想要的是利用 UIGestureRecognizerDelegate 中的 shouldRecognizeSimultaneous With。
但是,当我尝试将两个 UIScrollViews 委托给同一个 ViewController 时,错误"UIScrollView 的内置平移手势识别器必须将其滚动视图作为其委托">出现在ScrollView_2上。
关于如何解决这个问题的任何想法?
更新
这是来自page_3 UIViewController的cellForRow,它不会导致委托错误,但UIScrollViews不会同时起作用。(例如,需要上下移动手指以检测其他UIScrollView。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "random", for: indexPath)
return cell
}
下面是导致"UIScrollView 的内置平移手势识别器必须将其滚动视图作为其委托的 cellForRow。
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
}
我解决了它.UIScrollView
的内置UIPanGestureRecognizer
必须委托给它自己的UIScrollView
,因为错误"UIScrollView 的内置平移手势识别器必须将其滚动视图作为其委托。 说.
而是对UIScrollView
进行子类化并创建第二个UIPanGestureRecognizer
,然后与内置UIPanGestureRecognizer
同时起作用。
步骤 1 - 4:
-
对单元格的
UIScrollView
进行子类化并创建第二个UIPanGestureRecognizer
。 -
将
UIGestureRecognizerDelegate
添加到CellScrollView
,并使shouldRecognizeSimultaneouslyWith
返回true
.
第二个UIPanGestureRecognizer
现在将与内置UIPanGestureRecognizer
同时起作用。
class 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
}
}
- 在cellForRow中,将新创建的UIPanGestureRecognizer委托给UIScrollView_1的UIViewController。
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
}
- 最后,在ScrollView_1的UIViewController中,让shouldRecognizeSimultaneous With返回true。
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我想你想使用类似的东西
func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
if page3 {
return true
}
}
在ScrollView_2的UIViewController上,并让ScrollView_2(不是手势)的委托作为ViewController,
ScrollView_2.delegate = self
UIPanGesture.delegate = ScrollView_2