Search code examples
iosuiscrollviewuiscrollviewdelegate

UIScrollView scrollViewDidEndDragging and scrollViewDidEndDecelerating not working


I have searched and found multiple posts that the recommended way to determine when a user stopped scrolling, and when a UIScrollView stopped moving is the following:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
   // Perform desired outcome here.
   NSLog(@"scrollViewDidEndDragging");
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
   // Perform desired outcome here.
   NSLog(@"scrollViewDidEndDecelerating");
}

I have tried multiple ways, I can not get it to work.

Is there something obvious I am missing? Something with the delegate?


Solution

  • Make sure that you have all the following code inside your ViewController.

    Under your ViewController.m add

    @interface ScrollViewTestViewController ()<UIScrollViewDelegate>
    

    Inside ViewDidLoad

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    
    self.scrollView1.delegate=self;
    self.scrollView2.delegate=self;
    }
    
    
    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
                             willDecelerate (BOOL)decelerate {
     // Perform desired outcome here.
     NSLog(@"scrollViewDidEndDragging");
    
     if(scrollView==self.scrollView1)
       //Do Something
     else if(scrollView==self.scrollView2)
       // Do Another thing
     }
    

    Add a Screen Shot and a content size to simulate the scrolling behaviour.

    enter image description here