gpt4 book ai didi

ios - setContentOffset : animated: wont do anything if animated = NO

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:48 34 4
gpt4 key购买 nike

我正在尝试滚动到在 webView 中查看的 PDF 的上次查看位置。 PDF卸载时,会保存webView的scrollView的y偏移量。然后当 PDF 重新打开时,我想跳到他们离开的地方。

下面的代码在 animated 设置为 YES 时工作正常,但是当它设置为 NO 时,什么也没有发生

    float scrollPos = [[settingsData objectForKey:kSettingsScrollPosition]floatValue];
NSLog(@"scrolling to %f",scrollPos);
[webView.scrollView setContentOffset:CGPointMake(0, scrollPos) animated:NO];
NSLog(@"ContentOffset:%@",NSStringFromCGPoint(webView.scrollView.contentOffset));

这个输出:

滚动到 5432.000000

CO:{0, 5432}

但是PDF仍然显示首页

我在这里查看了类似问题的答案,但他们没有解决这个问题。

感谢您的帮助:)

最佳答案

UIWebView 组件完成 PDF 渲染之前,您不能触摸 contentOffset。它适用于 setContentOffset: animated:YES 因为动画会强制渲染。

如果在渲染开始后将contentOffset设置为至少0.3s(根据我的测试),则完全没有问题。

例如,如果您在 UIViewControllerviewDidLoad 中加载 PDF,您可以在 中使用 performSelector:withObject:afterDelay: >viewDidAppear:延迟contentOffset设置。

要在设置 contentOffset 之前隐藏 PDF,您可以将其 alpha 设置为 0.01(不要将其设置为 0,除非渲染不会开始)并在设置后将其设置回 1 contentOffset

@interface ViewController : UIViewController
{
UIWebView *w;
}

@property (nonatomic, retain) IBOutlet UIWebView *w;

@end

@implementation ViewController

@synthesize w;

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *u = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"];
[w loadRequest:[NSURLRequest requestWithURL:u]];
w.alpha = 0.01f;
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(adjust) withObject:nil afterDelay:0.5f];
}

- (void)adjust
{
float scrollPos = 800;
NSLog(@"scrolling to %f",scrollPos);
[w.scrollView setContentOffset:CGPointMake(0, scrollPos) animated:NO];
NSLog(@"ContentOffset:%@", NSStringFromCGPoint(w.scrollView.contentOffset));
w.alpha = 1;
}

@end

关于ios - setContentOffset : animated: wont do anything if animated = NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730815/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com