0

I have this delegate which intended to be execute when user scrolls with his fingers :

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

I have this line that scroll with code to the start :

[scroller scrollRectToVisible:CGRectMake(0, 0,scroller.frame.size.width,scroller.frame.size.height) animated:YES];

When i execute this line of code to scroll programmatically, the first delegate is also being called. I would like to eliminate this from happen.

I was trying to set the delegate to nil, but it still execute the delegate:

scroller.delegate=nil;//to not excute scrolldidscroll
[scroller scrollRectToVisible:CGRectMake(0, 0,scroller.frame.size.width,scroller.frame.size.height) animated:YES];
scroller.delegate=self;

EDIT:

I have read this great answer :Setting contentOffset programmatically triggers scrollViewDidScroll but it seems that his solution(the first) is without animation, while i need that animation .

Community
  • 1
  • 1
Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

7

There might be a better solution but this should work :

Declare a boolean in your .h

@property(nonatomic,assign) BOOL scrollingProgrammatically;

And in your .m :

self.scrollingProgrammatically = YES;
[scroller scrollRectToVisible:CGRectMake(0, 0,scroller.frame.size.width,scroller.frame.size.height) animated:YES];

...

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(!self.scrollingProgrammatically){
        ...
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    self.scrollingProgrammatically = NO;
}
streem
  • 9,044
  • 5
  • 30
  • 41