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!
I had this happen too and ended up doing this workaround:
[UIView animateWithDuration:.25 animations:^{
self.scrollView.contentOffset = ...;
}];
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.
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:
Creation of UIScrollView with nib.
-(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With