简体   繁体   中英

How can I detect when the user tried to swipe “past UIScrollView content offset”?

For example, I have a button that when the user tap on it, it will open a pop up scroll view from the bottom, which has a lengthy vertical content. If the user tried to swipe down when the scroll view content offset is at the top, I want to close the scroll view pop up by animating it flying to the bottom of the screen.

How can I detect that event? I know that I can get the event when scroll view did scroll using scrollViewDidScroll function. But it didn't give me any info of whether the user is scrolling up or down. So I can't check like "when the offset.y is 0 and user is still swiping .down then close the popup". Please help. Thanks.

Try this in scrollViewDidScroll(_ scrollView: UIScrollView) method

let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset

let absoluteTop: CGFloat = 0;
let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height;

let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom
self.previousScrollOffset = scrollView.contentOffset.y

and set this as global variable var previousScrollOffset: CGFloat = 0;

you can print and check if this is scolling up or down

hope this will give you the direction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM