UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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!

3      

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.

5      

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

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.