0

My app uses a PageViewController, 3 NavigationControllers and 3 ViewControllers. You can swipe from one VC to another. See animated gif.

In the VC's there are TableViews with leading and trailingSwipeActions. To my regret the leading swipe action works only on the first VC. The trailing swipe action only works on the last VC.

A solution would be if the swipe of the PageViewController only works in the area of the picture, and not in the area of the tableviews.

For the leading and trailing swipe action, just a small movement is sufficient. For the PageViewController swipe it must be a big movement. I don't know if that is something to proceed.

Found a lot of questions and some examples, but I can't get them to work on my use case.

The code of the 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

The trailing swipe action code:

    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])
}
arakweker
  • 1,535
  • 4
  • 18
  • 40

1 Answers1

0

UIGestureRecognizer has a property named cancelsTouchesInView. Set it to false so that both gestures will be called.

Toma
  • 2,764
  • 4
  • 25
  • 44
  • can you please be a bit more specific on how I should do that? Guess I have to do that in the VC's and not in the PageViewController. So in the code of the leading and trailing swipe actions? Thanks. – arakweker Jan 28 '18 at 15:57
  • I've added two gesture recognizers in the VC / TableView storyboard. And then in behaviour (Attributes Inspector) disabled 'cancel touches in view'. That didn't help sofar. – arakweker Jan 28 '18 at 16:13
  • You disabled it for both of them? – Toma Jan 28 '18 at 16:14
  • Yep, both for left and right swipes 'cancel touches in view' is disabled. – arakweker Jan 28 '18 at 16:15
  • Can you attach screenshots with the storyboard, please? – Toma Jan 28 '18 at 16:59
  • I've added some screenshots. Let me know what you need more :-) – arakweker Jan 28 '18 at 18:38
  • Have you tried adding the gesture recognizers just programmatically? – Toma Jan 28 '18 at 18:40
  • In my opinion I've added the trailing and leading swipe actions programmaticaly... see added code in question. – arakweker Jan 28 '18 at 18:47
  • See this question: https://stackoverflow.com/questions/7788780/uipageviewcontroller-gesture-recognizers – Toma Jan 28 '18 at 18:50
  • I've seen that. There they have taps in te VC and swipes in the PVC. I don't see how I can solve my issue. Guess I've to look for a different use case. – arakweker Jan 28 '18 at 19:38