简体   繁体   English

setContentOffset 的问题:动画:YES

[英]Problem with setContentOffset:animated:YES

I'm having a problem with what almost seems like a bug in iOS.我遇到了 iOS 中几乎看起来像错误的问题。 I'm trying to do some really simple scrolling in a UIScrollView.我正在尝试在 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 .如果我使用 animation 滚动到一个点,它会很好地滚动到那里,但它不会将该点设置为 scrollView IE when I scroll to somewhere else later, it jumps up to 0,0 and starts the animation from there. IE 当我稍后滚动到其他地方时,它会跳到 0,0 并从那里启动 animation。

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生产 output

offset x 0
offset y 0

while the exact same code with the animation off:而与 animation 关闭的代码完全相同:

[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生产 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.我正在尝试自动滚动到 UITextView,所以我正在收听一些我通常进行滚动的键盘通知。 But I've done this test in viewDidLoad and it produces these results.但是我已经在 viewDidLoad 中完成了这个测试,它产生了这些结果。

Doing scrollView.contentOffset = CGPointMake(0,95);做 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.它将视图滚动到正确的 position。 But since it doesn't seem to set contentOffset correctly it starts the animation from 0,0 all the time.但由于它似乎没有正确设置 contentOffset 它总是从 0,0 开始 animation 。 Now matter how long I wait between the animations.现在无论我在动画之间等待多长时间。

I would say both Gabriele's and Dani's answers are right.我想说Gabriele 和Dani 的答案都是正确的。 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.您基本上启动了另一个线程,该线程为您的 scrollView 运动设置动画,然后您立即尝试获取更新的值。 They'll be updated by that second thread, and you can catch them in some delegate function, eg scrollViewDidScroll:, scrollViewDidEndScrollingAnimation: etc.它们将由第二个线程更新,您可以在一些委托 function 中捕获它们,例如 scrollViewDidScroll:、scrollViewDidEndScrollingAnimation: 等。

BUT if you're saying, that eventually your position isn't being changed, then problem, I guess, is not here.但是,如果您说,最终您的 position 没有被更改,那么我想问题不在这里。 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.在您的 tableViewController class 中。 And if you do, you should put the code with setOffset in its body instead of viewDidLoad.如果你这样做了,你应该将带有 setOffset 的代码放在它的主体中,而不是 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. ---如果您的 textView 被放置在某种弹出窗口 window 上,例如 UIAlertView,并且您想正确放置它,您应该看看 setTransform: UIAlertView 的方法。 But as far as I know the problem with it is fixed in 4.2 version.但据我所知,它的问题已在 4.2 版本中得到修复。

The value is set while the animation is going progressively.该值是在 animation 逐步运行时设置的。 so if you check right after it started it will be on 0, check after few seconds and it will be fine.因此,如果您在启动后立即检查,它将为 0,几秒钟后检查就可以了。

Setting animated:YES you use the CoreAnimation instead of set your contentOffset instantly.设置动画:是的,您使用 CoreAnimation 而不是立即设置 contentOffset。

try this:尝试这个:

[scrollView setDelegate:self];

and then接着

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM