0

How can I make a UIScrollView start scrolling from top to bottom. When i swipe(down) it will scroll down first not up and disappear. What i have now is, i can scroll up first and then down, while scrolling down it's disappear. Here is my code -

func scrollViewDidScroll(_ scrollView: UIScrollView) {

        //for toogle swipe view
        if scrollView.contentOffset.y < 120 {
            self.swipeMsgView.isHidden = false
        }else {
            self.swipeMsgView.isHidden = true
        }
        //for making bottom inset
        if scrollView.contentOffset.y < 190 {
            var contentInset:UIEdgeInsets = scrollView.contentInset
            contentInset.bottom = scrollView.contentOffset.y-100
            scrollView.contentInset = contentInset
        }
        //when swipe down
        if scrollView.contentOffset.y == 0 {
            if !isScrollDownFirstTime{
                UIView.animate(withDuration: 0.5, animations: {
                    self.dismiss(animated: true, completion: nil)
                })
            }
        }
        //for tracking first time scrolling
        if scrollView.contentOffset.y > 150 {
            isScrollDownFirstTime = false
        }
    }

It works fine. But i want to disappear the view when a user swipes or scroll down first (swipe gesture is not working). Is there any elegant way to do this with this existing feature?Thank you.

Rashed
  • 2,349
  • 11
  • 26
  • Sorry, just confused with what you are looking for exactly. Do you mean that the instant the user starts swiping down you want to hide this view? Also, do you want to make the view re-appear when the scrollview is swiped up or when it gets to the top? – Alan May 30 '18 at 08:56
  • @AlanS yes , when user starts swiping down i want to hide/dismiss the view and show them another view controller. and your second query is already implemented. – Rashed May 30 '18 at 08:59
  • Have a look at the link at the end of my comment. Maybe you can use the link to get the speed of the scrollView scrolling. They are getting the absolute value of it so you might want to not use absolute so as to see if the speed is positive or negative, which can be used to tell the direction. At the start of your function you can check for the speed, and if it is going downwards, then show and hide your views based on this. https://stackoverflow.com/questions/3719753/iphone-uiscrollview-speed-check – Alan May 30 '18 at 09:02
  • thanks for your help. but my question was i want to dismiss the view while user first scroll/swipe it down not up. i can measure the scrollView.contentOffset.y from my current code :) – Rashed May 30 '18 at 09:05
  • My comment was based on the fact that you can check on the speed and not a set offset. If you see that the speed is moving downwards at the start of the function then you can hide the view. Also, you're dismiss code should not be in a UIView.animate block, it will already animate on it's own. That could be your issue actually. – Alan May 30 '18 at 09:09
  • Present scenario- user can scroll up and down, while scrolling down it disappear automatically. What i want- user can dismiss the view while swiping down first time. but i could not get it down because can't move the view down. Hope u will understand the problem. If i can't move it down then how could i dismiss the view. @AlanS – Rashed May 30 '18 at 09:17
  • 1
    Did you find the reason? I have the same problem and can't detect the reason. Is it because of autolayout? – Serg Smyk May 19 '21 at 22:10
  • @SergSmyk Please see the code that I have shared. It might solve your problem – Rashed May 20 '21 at 05:00

1 Answers1

0

@Serg Smyk, Here how I fixed the problem.

    fileprivate var isScrollDownFirstTime = true

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("Content y offet : \(scrollView.contentOffset.y)")
        //for toogle swipe view
        if scrollView.contentOffset.y < 120 {
            self.swipeMsgView.isHidden = false
        } else {
            self.swipeMsgView.isHidden = true
        }
        
        //for making bottom inset
        if scrollView.contentOffset.y < 190 {
            var contentInset:UIEdgeInsets = scrollView.contentInset
            contentInset.bottom = scrollView.contentOffset.y-100
            scrollView.contentInset = contentInset
        }
        
        //when swipe down
        if scrollView.contentOffset.y == 0 {
            print(CLASS_NAME+" -- scrollViewDidScroll() -- contentOffset.y = 0")
            if !isScrollDownFirstTime{
                UIView.animate(withDuration: 0.5, animations: {
                    self.dismiss(animated: true, completion: nil)
                })
            }
        }

        if scrollView.contentOffset.y < 1 {
            print(CLASS_NAME+" -- scrollViewDidScroll() -- contentOffset.y<0")
            UIView.animate(withDuration: 0.5, animations: {
                self.dismiss(animated: true, completion: nil)
            })
        }
        //for tracking first time scrolling
        if scrollView.contentOffset.y > 150 {
            isScrollDownFirstTime = false
        }
    }
Rashed
  • 2,349
  • 11
  • 26