标签:ios objective-c uiwebview 查看大图 点击缩放
UIWebView 点击查看大图,参考示例如下:
#import <UIKit/UIKit.h> @interface ShowBigImgViewController : UIViewController //UIWebView @property (weak, nonatomic) IBOutlet UIWebView *webDetails; @end
#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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:ios objective-c uiwebview 查看大图 点击缩放
原文地址:http://blog.csdn.net/yimiyuangguang/article/details/47335925