0
votes

While I used the method "scrollRectToVisible", the value of the property "contentOffset" remained the same although the frames visually were moved in Scroll View. I thought that "contentOffset" reflects how much it is moved/scrolled in Scroll View. Here is my code:

NSLog(@"Offset Before: %f", scrollView.contentOffset.x);      
       CGRect rectToScroll = CGRectMake(
                                  100,
                                  subview.frame.origin.y,
                                  subview.frame.size.width,
                                  subview.frame.size.height);
[scrollView scrollRectToVisible:rectToScroll animated:YES];
NSLog(@"Offset After: %f", scrollView.contentOffset.x);

"subview" represents UIImageView inside UIScrollView, but for the question it does not matter.

1

1 Answers

2
votes

You checked offset before actual scroll happened. contentOffset property reflects current state, not something that will be in the future. The scrollRectToVisible only starts animation which will end some time later. I'm not sure if setting animated to NO will immediately scroll (it can set contentOffset directly or start animation with 0 time which may or may not be processed immediately). The easiest way to see that the contentOffset does actually change is to implement -(void)scrollViewDidScroll:(UIScrollView*)scrollView; of scroll view delegate. Or do something like this for quick check (note that this 1 second is arbitral).

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    NSLog(@"Offset After: %f", scrollView.contentOffset.x);
});