UIWebView简单使用:加载进度条,加载PDF文件

来源:互联网 发布:sdr软件怎么使用 编辑:程序博客网 时间:2024/06/06 01:57

UIWebView的使用是很常见的,现在将它的一些使用方式总结如下:

1)首先是最简单的加载一个URL(网页链接):

  UIWebView *webView = [[UIWebViewalloc]init];

webView.frame =CGRectMake(0,KNavigationHeight,KScreenWidth,KScreenHeight-KNavigationHeight);

    NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.baidu.com"]];

    [webView loadRequest:request];

    [self.viewaddSubview:webView];

这样就可以加载出来百度的首页了。

2)有些比较大的网页,加载需要一些时间,这个时候我们就可以给网页的加载过程添加一个进度条,很多时候其实我们并没有必要按它真实的加载过程显示进度条,故可以使用假的进度条,模拟网页正在加载,做法如下:

自定义一个类,继承自UIView,就叫MyProgressView吧,它.h文件中如下

@interface MyProgressView : UIView

//进度条的颜色

@property (nonatomic,strong)UIColor *progressViewColor;

//进度条的高度

@property(nonatomic,assign)CGFloat progressViewHeight;

//开始加载进度条

-(void)startLoadData;

//结束加载进度条

- (void)finishLoadData;

@end


在.m文件中实现

static CGFloat progressViewY =64;


@implementation MyProgressView

{

    UIView *_progressView;

}


- (instancetype)init

{

    if (self = [superinit]) {

        self.frame =CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width,2);

    }

    return self;

}


-(void)startLoadData

{

    _progressView = [[UIViewalloc]init];

    _progressView.frame =CGRectMake(0,progressViewY,0,self.progressViewHeight);

    _progressView.backgroundColor =self.progressViewColor;

    [UIViewanimateWithDuration:3animations:^{

        _progressView.frame =CGRectMake(0,progressViewY,self.frame.size.width-40,2);


    } ];

    [selfaddSubview:_progressView];

}


- (void)finishLoadData

{

    [UIViewanimateWithDuration:0.5animations:^{

        _progressView.frame =CGRectMake(0,progressViewY,self.frame.size.width,2);

        [selfremoveFromSuperview];

    }];

}


这样我们就完成了一个假的进度条,使用按如下方法,将其放在viewDidLoad方法中:

    MyProgressView *progressView = [[MyProgressViewalloc]init];

    self.progressView = progressView;

    progressView.progressViewColor = [UIColorredColor];

    progressView.progressViewHeight = 2;

    [progressView startLoadData];

    [self.viewaddSubview:progressView];

然后实现设置webView的代理为本控制器,实现代理方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    [self.progressViewfinishLoadData];

}

网页整体加载完成后就干掉这个progressView,简单实现了加载进度条。

我们可以自定义进度条的颜色和高度等。

3)实现webView顶部下拉后面显示某些内容(本类中显示一个label)

这个功能实现起来比较简单,直接上代码:

    UIWebView *webView = [[UIWebViewalloc]init];

    //随便添加的一个label,仅供实验~

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,300, 50)];

    label.text = @"good idea";

    [webView addSubview:label];

    [webView bringSubviewToFront:webView.scrollView];

4)利用webView加载PDF文件

UIWebView *webView = [[UIWebViewalloc] init];

    webView.frame =CGRectMake(0,KNavigationHeight, KScreenWidth,KScreenHeight-KNavigationHeight);

    //url是服务器传的PDF链接url或者本地文件的路径

//NSURL *url = [[NSBundlemainBundle] URLForResource:@"xxxxx.docx"withExtension:nil];

    NSURL *url = [NSURLURLWithString:self.urlString];

    NSURLRequest *request = [NSURLRequestrequestWithURL:url];

    [webView loadRequest:request];

    webView.scalesPageToFit = YES;//使PDF文件可以完全适配屏幕显示

    [self.viewaddSubview:webView];


0 0
原创粉丝点击