Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with uiscrollview setcontentoffset animated not scrolling when animated:yes is set

This is very odd and I'm wondering if anyone has any thoughts?

I'm trying to scroll a UIScrollView in response to a button press on the iPad.

If I do:

CGPoint currentOff = scrollView.contentOffset;
currentOff.x+=240;
[scrollView setContentOffset:currentOff animated: NO];

The scroll view jumps to the required position as expected, but I want it to scroll. However, when I do:

CGPoint currentOff = scrollView.contentOffset;
currentOff.x+=240;
[scrollView setContentOffset:currentOff animated: YES];

Then it doesn't do anything! I have another scroll view which is working properly and responds to setContentOffset:YES as expected, so I am quite puzzled. Any ideas on why scrolling might not happen are most welcome!

Also, - (void)scrollViewDidScroll:(UIScrollView *)sender is not receiving anything at all when animated:YES is used but is invoked when animated:NO is used!

like image 397
BountyBob Avatar asked Oct 15 '10 10:10

BountyBob


3 Answers

I had this happen too and ended up doing this workaround:

[UIView animateWithDuration:.25 animations:^{
    self.scrollView.contentOffset = ...;
}];
like image 74
Philippe Sabourin Avatar answered Oct 17 '22 12:10

Philippe Sabourin


Speaking from personal experience, this problem occurs when setContentOffset: is getting called multiple times before the animation has had a chance to run.

I haven't totally tracked down the issue, but I believe the problem goes something like this:

setContentOffset:animated gets called which sets the new offset value. setContentOffset: subsequently gets called with the same parameters, which cancels the animation. It also notices that the new parameters are the same as the old parameters so assumes nothing needs to be done, even though the animation hasn't run yet.

The solution above somehow breaks this cycle, but perhaps a less kludgy solution is to put break points in the code and eliminate the multiple calls.

like image 25
aepryus Avatar answered Oct 17 '22 10:10

aepryus


Hard to say with that piece of code you gave. And in fact, if you're saying, that you have a project, where everything is working under the same conditions, then obviously the conditions aren't the same :) I couldn't reproduce the issue, but here are some assumptions for 2 cases:

  1. Creation of UIScrollView with nib.

    • Just don't forget to create IBOutlet and to bound it to the scrollView element in nib. In a simple view based application just after that I got scrollView working as you wished in animated: both YES and NO. But to make work

    -(void)scrollViewDidScroll:(UIScrollView *)sender; you must set the delegate:

yourScrollView.delegate = self;

where 'self' is class, where you have your scrollViewDidScroll function; btw this class must conform to the 'UIScrollViewDelegate' protocol.

2.Creation of UIScrollView object manually.

  • here the idea is basically the same, so I'll just provide some simple code:

    -(void) creatingScrollView {
        UIScrollView *newScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 5, 280, 44)];
        newScroll.pagingEnabled = YES;
        newScroll.showsVerticalScrollIndicator = NO;
        newScroll.showsHorizontalScrollIndicator = NO;
    
        // ... some subviews being added to our scroll view
    
        newScroll.delegate = self;
        [self.view addSubview:newScroll];
        [newScroll release];
    }
    

This works for me. Hope this is helpful.

like image 2
makaron Avatar answered Oct 17 '22 12:10

makaron