我的应用使用了PageViewController,3个NavigationControllers和3个ViewControllers。您可以从一个VC滑动到另一个VC。看动画gif。
在VC中有TableViews,其中包含leading和trailingSwipeActions。令我遗憾的是,领先的滑动动作仅适用于第一个VC。尾随滑动操作仅适用于最后一个VC。
解决方案是,如果PageViewController的滑动仅适用于图片区域,而不是在tableviews区域。
对于前导和尾随滑动动作,只需一个小动作即可。对于PageViewController滑动,它必须是一个很大的运动。我不知道这是否可以继续。
发现了很多问题和一些例子,但我无法让他们处理我的用例。
PageViewController的代码:
class PageViewController: UIPageViewController, UIPageViewControllerDataSource {
lazy var viewControllerList: [UIViewController] = {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "NavBirdVC")
let vc2 = sb.instantiateViewController(withIdentifier: "NavChickVC")
let vc3 = sb.instantiateViewController(withIdentifier: "NavEggVC")
return [vc1, vc2, vc3]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
if let firstViewController = viewControllerList.first {
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllerList.count > previousIndex else {return nil}
return viewControllerList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else {return nil}
guard viewControllerList.count > nextIndex else {return nil}
return viewControllerList[nextIndex]
}
}
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
[1]: https://i.stack.imgur.com/w9dK6.png
[2]: https://i.stack.imgur.com/Dk7jN.png
[3]: https://i.stack.imgur.com/DtRyu.png
尾随滑动操作代码:
override func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let optionMenu = UIAlertController(title: nil, message: "Weet u zeker dat u dit kuiken wilt verwijderen?", preferredStyle: .actionSheet)
let delete2Action = UIAlertAction(title: "Verwijder kuiken \(self.araKuikens[indexPath.row].roepNaam)", style: .destructive, handler: {(alert: UIAlertAction!) -> Void in
self.araKuikens[indexPath.row].deleteItem()
self.araKuikens.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
})
let archiveerAction = UIAlertAction(title: "Archiveer kuiken \(self.araKuikens[indexPath.row].roepNaam)", style: .default, handler: {(alert: UIAlertAction!) -> Void in
var araKuiken = self.araKuikens[indexPath.row]
araKuiken.markAsCompleted()
self.araKuikens.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
})
let cancelAction = UIAlertAction(title: "Annuleer", style: .cancel, handler: {(alert: UIAlertAction!) -> Void in
})
optionMenu.addAction(delete2Action)
optionMenu.addAction(archiveerAction)
optionMenu.addAction(cancelAction)
self.present(optionMenu, animated: true, completion: nil)
success(true)
})
let shareAction = UIContextualAction(style: .normal, title: "Share", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
success(true)
})
deleteAction.image = UIImage(named: "Trash")
deleteAction.backgroundColor = .red
shareAction.image = UIImage(named: "Share")
shareAction.backgroundColor = .orange
return UISwipeActionsConfiguration(actions: [deleteAction,shareAction])
}
override func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let weightAction = UIContextualAction(style: .normal, title: "Gewicht", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
self.index = indexPath.row
self.performSegue(withIdentifier: "weightSegue", sender: ac)
success(true)
})
weightAction.image = UIImage(named: "Scale")
weightAction.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1)
return UISwipeActionsConfiguration(actions: [weightAction])
}