UIScrollView does not scroll to the desired location using setContentOffset: animated:

I have a series of images in a UIScrollView. When the user releases the scroll action, I want the view to be centered on one image. I have implemented the code, but the basic behavior is incorrect. My call to setContentOffset: animated: has the correct values, but the view does not actually scroll to this point. Here is the output of some debugging:

2011-03-26 17:28:16.201 PivotMachine[39984:207] Dragging Beginning at: 270, 0 2011-03-26 17:28:16.322 PivotMachine[39984:207] Dragging Ended at: 360, 0 --> 350, 0 2011-03-26 17:28:16.624 PivotMachine[39984:207] Scrolling Ended at: 350, 0 2011-03-26 17:28:25.648 PivotMachine[39984:207] Dragging Beginning at: 447, 0 

As you can see, internally he thought that he had stopped pulling 360, so he properly advanced 350, where it should have been. Visually, he sits around 450, which he knows about when I start to drag again (yes, I know that you don’t start right away, so I drag very slowly to move a couple of pixels before the event).

This is the drag and drop code (for my subclass of UIScrollView):

 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { CGPoint myLocation = self.contentOffset; NSLog(@"Dragging Beginning at: %3.0f, %3.0f", myLocation.x, myLocation.y); } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollV { CGPoint myLocation = self.contentOffset; NSLog(@" Scrolling Ended at: %3.0f, %3.0f", myLocation.x, myLocation.y); } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { // Auto-scroll to the center of the closest menu if (scrollView != self) { DLog(@"[ScrollMenu scrollViewWillBeginDecelerating] called with non-self"); return; } CGPoint myLocation = self.contentOffset; int idx = [self indexOfMenuClosestTo:myLocation]; CGPoint menuPoint = CGPointMake(menuPoints[idx], self.contentOffset.y); NSLog(@" Dragging Ended at: %3.0f, %3.0f --> %3.0f, %3.0f", myLocation.x, myLocation.y, menuPoint.x, menuPoint.y); [self setContentOffset:menuPoint animated:YES]; } 

I should note that this is in the simulator. I still can not try it on the phone itself, since I have not yet spent money on the dev program (waiting until the application is almost ready for publication in case I lose a couple).

Could this be a simulator problem? Has anyone seen something like this? Yes, I have to ask: am I doing something wrong ?;)

Follow Up: I think the problem is that the slowdown animation contradicts my call -(void) setContentOffset:animated: Both animations continue to work, and the view is displayed depending on what wins, but the internal state is correctly set to setContentOffset . I thought setContentOffset turned off the existing animation. This, and if not, how can I turn it off?

+7
source share
1 answer

Try using - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated ( docs ) instead of setting contentOffset.
Otherwise, I can suggest checking the paging property of UIScrollView, although it may be too limited to do what you want.

+1
source

All Articles