Search code examples
iphonexcodexcode4

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.


Solution

  • 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";
    }