BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Swiping to reveal previous/next entries without going back to UITableView

Forums > Swift

OK, so an app I'm working on is going to have a dictionary-like section, and I'm trying to set it up so the user can swipe to see the next/previous without having to add next/previous buttons to the layout or send the user back to the main UITableView to choose another row.

I'm working on building out the UI at the moment, and I've got a 90% solution to what I'm trying to do for that view here:

import UIKit

class CharacterDetailViewController: UIViewController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "Character Detail View"

        // Disable swipe-to-pop gesture
        navigationController?.interactivePopGestureRecognizer?.delegate = self
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false

        // Detect swipe gesture to load next entry
        view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(swipeNextEntry)))
    }

    @objc func swipeNextEntry(_ sender: UIPanGestureRecognizer) {
        print("[DEBUG] Pan Gesture Detected")

        if (sender.state == .ended) {
            let velocity = sender.velocity(in: self.view)
            guard let vc = storyboard?.instantiateViewController(identifier: "CharacterDetail") else { return }

            if (velocity.x > 0) { // Coming from left
                navigationController?.pushViewController(vc, animated: true)
            } else { // Coming from right
                navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}

The issue I'm having is that no matter which direction you swipe from, the new view controller always comes in from the right side of the screen. I've been trying to figure out how to make new controller push in from the left side instead, and I'm not getting very far. I saw something earlier suggesting using a segue to configure how the new views appear, but IB won't allow you to set something to segue to itself from what I can tell.

Any thoughts on how to proceed? Thanks!

1      

Hi, I dont think using view controllers is the best option here, because with each push you are going to build up the controllers stack and the default back behaviour (when user wants to return to the Table View) will not work. I would personally use horizontal Collection View with paging enabled.

So in effect one cell will be the whole screen and you can then swipe left/right to move through the collection view. You don't have to worry about memory issues or disabling back navigation.

3      

Interesting idea! I'll see about giving that a shot and report back on how it goes

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.