OGeek|极客世界-中国程序员成长平台

标题: objective-c - 如何在 UIWebView 中跳转 pdf 页面? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:07
标题: objective-c - 如何在 UIWebView 中跳转 pdf 页面?

我正在尝试逐页滚动 pdf 文件

这是我的代码

-(void)viewDidLoad{

NSString *path = [[NSBundle mainBundle] pathForResource"myPdffile" ofType"pdf"];

NSURL *url = [NSURL fileURLWithPath:path];

NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];

[webview loadRequest:urlrequest];

webview.scalesPageToFit = YES;

[super viewDidLoad];

}

//goto next page

-(IBAction)nextpage{

        [webview stringByEvaluatingJavaScriptFromString: ?????? ];
}

//goto previous page

-(IBAction)prevpage {

       ?????
}

我应该在 stringByEvaluatingJavaScriptFromString 之后放什么?

我是否将相同的代码放在“prevpage”中?

谢谢!



Best Answer-推荐答案


在 iOS 5 中可以使用该语句进行页面跳转

[[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];

在iOS 5以下你可以使用这个语句

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat"window.scrollTo(0.0, %f)", y]];

y 是你要跳跃的高度。

我正在为下一页和上一页编写一些示例代码

-(IBAction) nextPage: (id) sender
{
    if (currentPage < numberOfPages) 
    {
        float y =  pageHeight * currentPage++;

        if (isIOSAbove4) 
        {
            [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
        }
        else
        {
            [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat"window.scrollTo(0.0, %f)", y]];
        }
    }
}

上一页:

-(IBAction) prevPage: (id) sender
{   
    if (currentPage - 1)
    {
        float y = --currentPage * pageHeight - pageHeight;
        if (isIOSAbove4) 
        {
            [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
        }
        else
        {
            [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat"window.scrollTo(0.0, %f)", y]];
        }
    }
}

在上面的示例代码中是IOSAbove4是在pch文件中定义的

#define isIOSAbove4 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)

希望以上代码能解决您的问题

关于objective-c - 如何在 UIWebView 中跳转 pdf 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416807/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4