2012-07-30 19 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