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 .

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok , i will go for it .. :)
hmm, yeah there is probably no lighter way to do this. Also I've edited my answer since didEndDragging is not called if scroll is called programmatically.
it actually did ! the dragging method(your first answer) did worked for me as well. strange .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.