Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollViewDidEndDecelerating not executing

I can't seem to get scrollViewDidEndDecelerating called. I have a scrollView with 2 Views inside. Now I need it to set a value to a label in the first view when the scrollview is finished scrolling to the second view.

Header File:

@interface ViewController: UIViewController
{
   UIScrollView *scrollView;
   UIView *view1;
   UIView *view2;
}

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UILabel *lbl;

Implementation File:

@synthesize scrollView, view1, view2;

-(void)viewDidLoad
{
   self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
   self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];

   [self.scrollView addSubView:self.view1];
   [self.scrollView addSubView:self.view2];

   self.scrollView.bounces=NO;
   self.scrollView.contentSize=CGSizeMake(640,460);
   [self.scrollView setShowHorizontalScrollIndicator:NO];
   [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
}

-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
   lbl.text=@"0";
}

I don't see anything wrong, it should be working. Can someone help me out? Would appreciate it.

like image 298
iamruskie Avatar asked Jun 11 '12 14:06

iamruskie


4 Answers

scrollViewDidEndDecelerating is not called if the user is scrolling slowly (i.e. the scroll view does not continue to scroll on touch up). In that case the delegate calls scrollViewDidEndDragging. So to do something when the user has stopped scrolling and the scrollview has stopped you can combine them:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  if !decelerate {
     endOfScroll()
  }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  endOfScroll()
}

func endOfScroll() {
  //The user finished scrolling
}

In Objective-C

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   if(!decelerate) [self endOfScroll];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  [self endOfScroll];
}

-(void)endOfScroll{
  //Do something
}
like image 197
Sten Avatar answered Oct 21 '22 04:10

Sten


Either connect the delegate property of the scrollview to the File's Owner object in Interface Builder or just set the delegate manually in your ViewController's ViewDidLoad.

scrollview.delegate = self
like image 36
Dcritelli Avatar answered Oct 21 '22 04:10

Dcritelli


This would do:

Header File:

@interface ViewController: UIViewController <UIScrollViewDelegate> //promise that you'll act as scrollView's delegate
{
   UIScrollView *scrollView;
   UIView *view1;
   UIView *view2;
}

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UILabel *lbl;

Implementation File:

@synthesize scrollView, view1, view2;

-(void)viewDidLoad
{
   self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
   self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];

   [self.scrollView addSubView:self.view1];
   [self.scrollView addSubView:self.view2];

   self.scrollView.bounces=NO;
   self.scrollView.contentSize=CGSizeMake(640,460);
   [self.scrollView setShowHorizontalScrollIndicator:NO];
   [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
   [self.scrollView setDelegate:self];//Set delegate
}

-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
   lbl.text=@"0";
}
like image 3
Ahmed Avatar answered Oct 21 '22 02:10

Ahmed


Swift 5

When scrolling UIScrollView programmatically, the scrollViewDidEndDecelerating is not called , but you can still use the scrollViewDidEndScrollingAnimation delegate function as a substitude for that, however there are some limitations stated in Apple documentation :

The scroll view calls this method at the end of its implementations of the setContentOffset(:animated:) and scrollRectToVisible(:animated:) methods, but only if animations are requested.

Therefore if you are not using default animated:true, but rather animating scrolling with custom UIView.animate animation (for example to change scrolling speed) , the only delegate method that still executes is scrollViewDidScroll

like image 1
Mr.SwiftOak Avatar answered Oct 21 '22 03:10

Mr.SwiftOak