Problem with setContentOffset:animated:YES
I'm having a problem with what almost seems like a bug in iOS开发者_开发技巧. I'm trying to do some really simple scrolling in a UIScrollView. If I scroll to a point with an animation, it scrolls there perfectly fine, but it doesn't set the point to the scrollView. I.E. when I scroll to somewhere else later, it jumps up to 0,0 and starts the animation from there.
I'm using the following code
[scrollView setContentOffset:CGPointMake(0, 95) animated:YES];
NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]);
NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]);
which produces output
offset x 0
offset y 0
while the exact same code with the animation off:
[scrollView setContentOffset:CGPointMake(0, 95) animated:NO];
NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]);
NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]);
produces output
offset x 0
offset y 95
I'm trying to automatically scroll to a UITextView so I'm listening to some keyboard notifications where I normally do the scrolling. But I've done this test in viewDidLoad and it produces these results.
Doing scrollView.contentOffset = CGPointMake(0,95); also sets the value correctly. It's just the animated one that doesn't.
Edit: The code I am actually trying to run is this:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//60 is half the available space in portrait mode so it puts the textfield in the centre.
[self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y-60) animated:YES];
}
Which scrolls the view to the correct position. But since it doesn't seem to set contentOffset correctly it starts the animation from 0,0 all the time. Now matter how long I wait between the animations.
I would say both Gabriele's and Dani's answers are right. In your first snippet you're actually trying to get final values before they're set. That is, when you invoke this
[scrollView setContentOffset:CGPointMake(0, 95) animated:YES];
you basically start another thread, which animates your scrollView movement, and right away you try to get updated values. They'll be updated by that second thread, and you can catch them in some delegate function, e.g. scrollViewDidScroll:, scrollViewDidEndScrollingAnimation: etc.
BUT if you're saying, that eventually your position isn't being changed, then problem, I guess, is not here. I think you provided info, which doesn't touch the problem itself.
---If you're working with table view, than you should check if you use
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
in your tableViewController class. And if you do, you should put the code with setOffset in its body instead of viewDidLoad.
---If your textView is placed on some kind of popup window, like UIAlertView, and you want to place it properly, you should take a look at setTransform: method of UIAlertView. But as far as I know the problem with it is fixed in 4.2 version.
The value is set while the animation is going progressively. so if you check right after it started it will be on 0, check after few seconds and it will be fine.
Setting animated:YES you use the CoreAnimation instead of set your contentOffset instantly.
try this:
[scrollView setDelegate:self];
and then
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Do something
}
精彩评论