2012-07-30 97 views
3

我想滚动到在webView中查看的PDF的最后查看位置。当PDF被卸载时,它将保存webView的scrollView的y偏移量。然后当PDF重新打开时,我想跳到他们离开的地方。当动画设置为YESsetContentOffset:animated:将不会做任何动画=否

下面的代码工作正常,但是当它被设置为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仍然是dis打首页

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

感谢您的帮助:)

+0

您是否尝试过调用'setNeedsDisplay'?只是一个想法。或者它将偏移设置为所需的一个像素而不是动画,然后将一个像素移动设置为所需位置的动画。那它有用吗? – James 2012-08-28 03:04:32

回答

1

你不能触摸contentOffset之前UIWebView成分已经做了PDF的渲染。它适用于setContentOffset: animated:YES,因为动画强制渲染。

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

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

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

@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