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

Dismiss detail view controller by scrolling on content #107

Open
arinasawa opened this issue Apr 1, 2019 · 4 comments
Open

Dismiss detail view controller by scrolling on content #107

arinasawa opened this issue Apr 1, 2019 · 4 comments

Comments

@arinasawa
Copy link

Is there a way to dismiss the detail view controller only by performing a gesture on the image view rather than the whole content of the detail view controller?

@PaoloCuscela
Copy link
Owner

PaoloCuscela commented Apr 1, 2019

Actually, I don't think so.
You can try preventing user interaction in the scroll view of DetailViewController file,
and then call dismiss() on backroungIV view pan gesture.
Let me know if you're having troubles ;)

@arinasawa
Copy link
Author

I can’t seem to figure out how to do what you said above? Do you have any example code that can achieve this?

@PaoloCuscela
Copy link
Owner

Forgive me if I have expressed myself badly, english isn't my first language.
Anyway, the detail view controller's view is composed of a scrollview that contains the card content on top and the view managed by a 3rd ViewController below.
So actually there isn't a way to do what you asked for, but you can try to achieve this by playing with the scroll view Delegate methods:

// Line 176 //MARK: - ScrollView Behaviour
extension DetailViewController: UIScrollViewDelegate {
    
  public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let y = scrollView.contentOffset.y
        let offset = scrollView.frame.origin.y - scrollViewOriginalYPosition
        
        // Behavior when scroll view is pulled down
        if (y<0) {
            scrollView.frame.origin.y -= y/2
            scrollView.contentOffset.y = 0
        
          // Behavior when scroll view is pulled down and then up
        } else if ( offset > 0) {
          
            scrollView.contentOffset.y = 0
            scrollView.frame.origin.y -= y/2
        }
        
        card.delegate?.cardDetailIsScrolling?(card: card)
    }
    
    public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        let offset = scrollView.frame.origin.y - scrollViewOriginalYPosition
        
        // Pull down speed calculations
        let max = 4.0
        let min = 2.0
        var speed = Double(-velocity.y)
        if speed > max { speed = max }
        if speed < min { speed = min }
        speed = (max/speed*min)/10
        
        guard offset < 60 else { dismissVC(); return }
        guard offset > 0 else { return }
        
        // Come back after pull animation
        UIView.animate(withDuration: speed, animations: {
            scrollView.frame.origin.y = self.scrollViewOriginalYPosition
            self.scrollView.contentOffset.y = 0
        })
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let offset = scrollView.frame.origin.y - scrollViewOriginalYPosition
        guard offset > 0 else { return }
        
        // Come back after pull animation
        UIView.animate(withDuration: 0.1, animations: {
            scrollView.frame.origin.y = self.scrollViewOriginalYPosition
            self.scrollView.contentOffset.y = 0
        })
    }
    
}

Some tips:
You could try to save the position of the first touch and block the scroll in the scrollviewdidscroll() method if the offset is > 0 and the starting position is inside the card content (BackgroundIV)

@arinasawa
Copy link
Author

OK because I am trying t get the card animation to look exactly like that on the app store where you can only dismiss the detail view controller by scrolling on the image view, no the whole content because then the user can easily/accidentally dismiss the view controller.

@PaoloCuscela PaoloCuscela changed the title Dismiss detail view controller Dismiss detail view controller by scrolling on content Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants