码迷,mamicode.com
首页 > Web开发 > 详细

UIWebView 点击查看大图

时间:2015-08-07 13:30:40      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:ios   objective-c   uiwebview   查看大图   点击缩放   

UIWebView 点击查看大图

UIWebView 点击查看大图,参考示例如下:

ShowBigImgViewController.h

#import <UIKit/UIKit.h>

@interface ShowBigImgViewController : UIViewController

//UIWebView
@property (weak, nonatomic) IBOutlet UIWebView *webDetails;

@end

ShowBigImgViewController.m

#import "ShowBigImgViewController.h"
#import "UIImageView+AFNetworking.h"

#define K_VIEW_DETAILS_URL @"http://3g.fx678.com/news"

@interface ShowBigImgViewController ()<UIGestureRecognizerDelegate>

@end

@implementation ShowBigImgViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //[S]设置网页显示内容
    NSURL *url = [NSURL URLWithString:K_VIEW_DETAILS_URL];
    [self.webDetails loadRequest:[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15.0f]];
    //[E]设置网页显示内容

    //[S]添加点击事件
    UITapGestureRecognizer *webTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.webDetails addGestureRecognizer:webTap];
    webTap.delegate = self;
    webTap.cancelsTouchesInView = NO;
    //[E]添加点击事件

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIGestureRecognizer Delegate
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

#pragma mark - Custom method
//根据点击的位置 获取原始图片地址
-(void) handleSingleTap:(UITapGestureRecognizer *) sender{

    CGPoint point =[sender locationInView:self.webDetails];

    NSString *strImgUrl = [NSString stringWithFormat:@"document.elementFromPoint(%f,%f).src;",point.x,point.y];
    strImgUrl = [self.webDetails stringByEvaluatingJavaScriptFromString:strImgUrl];
    if (![strImgUrl isEqualToString:@""])
        [self showBigImage:strImgUrl withPoint:point];

    int len = [self getHistorysLength];
    if (len > 0) {
        //头部导航栏添加返回按钮
        UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"but_back"] style:UIBarButtonItemStyleDone target:self action:@selector(returnDetails:)];
        self.navigationItem.leftBarButtonItem = leftBarBtn;
    }else
        self.navigationItem.leftBarButtonItem = nil;

    NSLog(@"strImgUrl:%@",strImgUrl);

}


//显示大图
-(void) showBigImage:(NSString *) strUrl withPoint:(CGPoint) point{

    if (![strUrl isEqualToString:@""]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(point.x, point.y, 0, 0)];
        [imageView setImageWithURL:[NSURL URLWithString:strUrl]];

        [self.view addSubview:imageView];
        [self.view bringSubviewToFront:imageView];  //最顶端

        [UIView animateWithDuration:.50f animations:^{
            imageView.frame = [[UIScreen mainScreen] bounds];
            imageView.center = self.view.center;
        } completion:^(BOOL finished) {
            //自适应屏幕
            imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

            imageView.userInteractionEnabled = YES; //否则图片点击不了
        }];

        //添加点击事件缩小
        UITapGestureRecognizer *tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBigImage:)];
        [imageView addGestureRecognizer:tapClick];

        //隐藏头部导航栏
        self.navigationController.navigationBarHidden = YES;
    }

}


//隐藏大图
-(void) hideBigImage:(UITapGestureRecognizer *) sender{

    if ([sender.view isKindOfClass:[UIImageView class]]) {

        UIImageView *imageView = (UIImageView *)sender.view;
        CGPoint point = [sender locationInView:self.view];

        [UIView animateWithDuration:.35 animations:^{
            CGRect rect = imageView.frame;
            rect.origin = point;
            rect.size = CGSizeZero;
            imageView.frame = rect;
        } completion:^(BOOL finished) {
            //移除
            [imageView removeFromSuperview];
        }];
    }

    //显示头部导航栏
    self.navigationController.navigationBarHidden = NO;

}


#pragma mark - UIWebView histors
//返回
-(void) returnDetails:(UIBarButtonItem *)sender{

    NSString *strHistory = nil;
    int len = [self getHistorysLength];
    if (len > 0) {
        //返回
        strHistory = @"window.history.back();";
        [self.webDetails stringByEvaluatingJavaScriptFromString:strHistory];

        NSLog(@"存在历史记录,可以返回!%d",len);
    }else{
        self.navigationItem.leftBarButtonItem = nil;

        NSLog(@"不存在历史记录");
    }

}

//获取历史记录数
-(int) getHistorysLength{

    NSString *strHistory = @"window.history.length;";
    return [[self.webDetails stringByEvaluatingJavaScriptFromString:strHistory] intValue];
}
@end

以上案例制作,code4app.com 源码网提供了大量帮助以示感谢

案例源码下载地址:

http://pan.baidu.com/s/1i3CionV

版权声明:本文为博主原创文章,未经博主允许不得转载。

UIWebView 点击查看大图

标签:ios   objective-c   uiwebview   查看大图   点击缩放   

原文地址:http://blog.csdn.net/yimiyuangguang/article/details/47335925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!