Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table view scroll #10

Closed
carson-katri opened this issue Jun 8, 2017 · 12 comments
Closed

Table view scroll #10

carson-katri opened this issue Jun 8, 2017 · 12 comments

Comments

@carson-katri
Copy link

I have implemented your scroll view code to my table view controller and it will dismiss when I’m not at the top. Do you have any suggestions?

Also when it said set the scroll view delegate as self, I just made sure tableview.delegate equaled self. Is that correct?

@anmolmalhotra
Copy link

anmolmalhotra commented Jun 8, 2017

UITableView is a subclass of UIScrollView. So I think you will have to implement the dismissal code too.
Have you tried this one (the one written in description):

Please note: I haven't tried this myself but I think this will work.

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard scrollView.isEqual(textView) else {
    return
 }

if let delegate = transitioningDelegate as? DeckTransitioningDelegate {
    if scrollView.contentOffset.y > 0 {
        // Normal behaviour if the `scrollView` isn't scrolled to the top
        scrollView.bounces = true
        delegate.isDismissEnabled = false
    } else {
        if scrollView.isDecelerating {
            // If the `scrollView` is scrolled to the top but is decelerating
            // that means a swipe has been performed. The view and scrollview are
            // both translated in response to this.
            view.transform = CGAffineTransform(translationX: 0, y: -scrollView.contentOffset.y)
            scrollView.transform = CGAffineTransform(translationX: 0, y: scrollView.contentOffset.y)
        } else {
            // If the user has panned to the top, the scrollview doesnʼt bounce and
            // the dismiss gesture is enabled.
            scrollView.bounces = false
            delegate.isDismissEnabled = true
        }
    }
 }
}

@carson-katri
Copy link
Author

carson-katri commented Jun 8, 2017 via email

@HarshilShah
Copy link
Owner

Hi there @CTKRocks. The code in the README should work as expected for you, provided you just replace the textView in the guard with your tableview’s variable. I haven’t used UITableView, but I’ve tested it with UIScrollView, UITextView, and UICollectionView, and it works perfectly in all of those.

I could take a look at your code if you could share it, since I can’t really gauge much just from the description alone.

@HarshilShah
Copy link
Owner

Closing due to inactivity

@carson-katri
Copy link
Author

The code in the Readme does not work.

@carson-katri
Copy link
Author

At least for TableView

@HarshilShah
Copy link
Owner

HarshilShah commented Aug 21, 2017

The code as it is does work with UITableView. I literally just replaced the UITextView in the example app with a simple UITableView and it all works. You can download the project folder, replace ModalViewController.swift's contents with this, and verify for yourself:

//
//  ModalViewController.swift
//  DeckTransition
//
//  Created by Harshil Shah on 15/10/16.
//  Copyright © 2016 Harshil Shah. All rights reserved.
//

import UIKit
import DeckTransition

class ModalViewController: UIViewController, UITableViewDelegate {

    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        modalPresentationCapturesStatusBarAppearance = true
        
        view.backgroundColor = .white
        
        tableView.showsVerticalScrollIndicator = false
        tableView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
		tableView.bounces = false
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
		
        tableView.dataSource = self
        tableView.delegate = self
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
	
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard scrollView.isEqual(tableView) else {
            return
        }
        
        if let delegate = transitioningDelegate as? DeckTransitioningDelegate {
            if scrollView.contentOffset.y > 0 {
                scrollView.bounces = true
                delegate.isDismissEnabled = false
			} else {
				if scrollView.isDecelerating {
					view.transform = CGAffineTransform(translationX: 0, y: -scrollView.contentOffset.y)
					scrollView.subviews.forEach {
						$0.transform = CGAffineTransform(translationX: 0, y: scrollView.contentOffset.y)
					}
				} else {
					scrollView.bounces = false
					delegate.isDismissEnabled = true
				}
			}
        }
    }
	
}

extension ModalViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "This is cell number \(indexPath.row)"
        return cell
    }
    
}

@carson-katri
Copy link
Author

Hmmm. Strange. For me using the code with my tableView replacing the textView does not work.

@HarshilShah
Copy link
Owner

Updated my comment above. You can check for yourself

@carson-katri
Copy link
Author

No. That code is not working for my case. Here's my code:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard scrollView.isEqual(self.tableView) else {
            return
        }
        
        if let delegate = transitioningDelegate as? DeckTransitioningDelegate {
            if scrollView.contentOffset.y > 0 {
                scrollView.bounces = true
                delegate.isDismissEnabled = false
            } else {
                if scrollView.isDecelerating {
                    view.transform = CGAffineTransform(translationX: 0, y: -scrollView.contentOffset.y)
                    scrollView.subviews.forEach {
                        $0.transform = CGAffineTransform(translationX: 0, y: scrollView.contentOffset.y)
                    }
                } else {
                    scrollView.bounces = false
                    delegate.isDismissEnabled = true
                }
            }
        }
    }

@HarshilShah
Copy link
Owner

My point was the code works for UITableView. The library functions as expected. I can't help you debug your code, sorry

@HarshilShah
Copy link
Owner

The override there means your view controller's superclass is implementing its own scrollViewDidScroll. Might be some conflict with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants