ios - 我的 pageviewcontroller 手势是否被拦截?
问题描述
我已按照 pageviewcontroller 教程创建图像滑块。然后我将其调整为插入到 tableviewcell 中,因为我想要表格中的图像滑块。
我喜欢在全屏或嵌入到另一个视图中使用相同的代码的想法。
但是,当我这样做时,pageviewafter 和 pageviewbefore 事件在滑动时不会触发。我怀疑 tableviewcontroller 或单元格正在吞噬事件,因此 pageviewcontroller 没有收到它们。
我充其量是兼职 ios 开发人员,所以寻找任何可以检查/克服我的问题的建议。
根据要求提供一些代码细节。在检索图像的回调完成后,我正在添加我的 PageViewController。PlantPhotoContainerView 是一个静态单元格内的 UIView,它与情节提要一起添加。
self.plantPhotoContainerView.addSubview(vc.view)
pageviewcontroller 将委托设置为自身,我目前正在忽略控制器中传递的图像,只使用一些测试图像:
class ImageSliderViewController: UIPageViewController, UIPageViewControllerDataSource {
init(images: [String]) {
super.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.horizontal, options: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var images = ["Growers", "Home", "Media"]
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
view.backgroundColor = UIColor.yellow
let imageViewController = ImageViewController()
imageViewController.imageName = images.first
let viewControllers = [imageViewController]
setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}
override func viewDidLayoutSubviews() {
self.view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: view.superview!.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: view.superview!.rightAnchor).isActive = true
view.topAnchor.constraint(equalTo: view.superview!.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: view.superview!.bottomAnchor).isActive = true
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentImageName = (viewController as! ImageViewController).imageName!
let currentIndex = images.firstIndex(of: currentImageName)!
if currentIndex < images.count - 1 {
let imageViewController = ImageViewController()
imageViewController.imageName = images[currentIndex + 1]
return imageViewController
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentImageName = (viewController as! ImageViewController).imageName!
let currentIndex = images.firstIndex(of: currentImageName)!
if currentIndex > 0 {
let imageViewController = ImageViewController()
imageViewController.imageName = images[currentIndex - 1]
return imageViewController
}
return nil
}
}
解决方案
好的,我最终找到了一个有帮助的帖子。添加子视图时,需要额外的两行才能将默认手势功能传递到 pageviewcontroller。希望这对其他人有帮助。
self.addChild(vc)
self.plantPhotoContainerView.addSubview(vc.view)
vc.didMove(toParent: self)
老实说,我不完全理解这是做什么的,也许更精通 swift 框架的人可以详细说明。
推荐阅读
- python - 将 df 保存为 csv 文件后丢失行
- sqlite - 如何在 Tkinter 中使用文本命令将值插入我的数据库?
- sql - SQL查询交叉引用同一张表
- r - 在与 Ubuntu Server 中的数据不同的文件夹中运行 R 脚本
- sql-server - 如何查找和替换 SQL Server 表中的所有值
- html - 边框半径不适用于右侧图像
- c# - 格式化来自模型的动态 json 字符串以在 .net 视图中显示
- ios - Xamarin VS2019 无法使用配对的 mac 测试 iOS 应用程序
- javascript - 将 number 替换为将原始值乘以 0.75 的数字
- java - Canvas 对象是否在两次不同的 onDraw() 方法运行之间发生变化?