Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when a UIScrollView has finished scrolling In Swift

There was, by all accounts, an excellent solution to this problem in Obj-C presented by Ashley Smart (How to detect when a UIScrollView has finished scrolling).

-(void)scrollViewDidScroll:(UIScrollView *)sender 
{   
[NSObject cancelPreviousPerformRequestsWithTarget:self];
    //ensure that the end of scroll is fired.
    [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.3]; 

...
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
...
}

I need a solution, however, in Swift.

It appears that the excellent delay function, contributed by Matt (dispatch_after - GCD in swift?) is likely to help.

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

and implemented as ...

delay(0.4) {
    // do stuff
}

but I've still not put it together. Any help?

like image 887
Don Hall Avatar asked Jun 01 '15 20:06

Don Hall


People also ask

How can I tell when a UIScrollView has finished scrolling?

scrollViewDidScroll only notifies you that the scroll view did scroll not that it has finished scrolling. The other method scrollViewDidEndScrollingAnimation only seems to fire if you programmatically move the scroll view not if the user scrolls.

What is contentOffset in ScrollView?

contentOffset is where the user is standing after scrolling the area. So this would change each and every time the user scrolls up and down. At times this can be set programmatically as well as in main thread, which will scroll up to given value if the position exists.

How do I scroll to the top of a Tableview table?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.


2 Answers

You need to check whether the user has stopped dragging and if the view is still decelerating after the user stopped dragging:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if collectionView.isDecelerating == false {
        // Perform whichever function you desire for when scrolling has stopped
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    // Perform whichever function you desire for when scrolling has stopped
}
like image 133
Ever Uribe Avatar answered Sep 20 '22 12:09

Ever Uribe


The delegate method tells you when finished

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    self.stoppedScrolling()
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate {
        self.stoppedScrolling()
    }
}

func stoppedScrolling() {
    println("Scroll finished")
}
like image 23
Nekak Kinich Avatar answered Sep 19 '22 12:09

Nekak Kinich