
思路:
1. webview加載url,js交互圖片插入點擊事件、獲取圖片數(shù)組和圖片寬高比數(shù)組.
2.點擊webview上的圖片,計算圖片在Windows上的位置,添加UIImageView到Windows上然后動畫全屏顯示,動畫結(jié)束后使用三方的圖片瀏覽
3.當(dāng)三方的圖片瀏覽消失后,判斷瀏覽的最后一張圖片是否是第一次點擊的圖片,如果是動畫返回原來的位置后移除,不是動畫透明消失后移除
1.插入js代碼,給圖片添加點擊事件
? ? [webViewstringByEvaluatingJavaScriptFromString:@"\
?? ? function assignImageClickAction(){\
?? ? var imgs=document.getElementsByTagName('img');\
?? ? var length=imgs.length;\
?? ? for(var i=0; i < length;i++){\
?? ? img=imgs[i];\
?? ? if(\"ad\" ==img.getAttribute(\"flag\")){\
?? ? var parent = this.parentNode;\
?? ? if(parent.nodeName.toLowerCase() != \"a\")\
?? ? return;\
?? ? }\
?? ? img.onclick=function(){\
?? ? getImage(this.src,this.getBoundingClientRect().top,this.clientWidth/this.clientHeight );\
?? ? }\
?? ? }\
?? ? };\
?? ? assignImageClickAction();"];
? ? //插入js代碼,獲取所有圖片寬高比
? ? id object = [webView stringByEvaluatingJavaScriptFromString:@"\
?? ? ? ? ? ? ? ? function getImages(){\
?? ? ? ? ? ? ? ? var objs = document.getElementsByTagName(\"img\");\
?? ? ? ? ? ? ? ? var imgScr = '';\
?? ? ? ? ? ? ? ? for(var i=0;i
?? ? ? ? ? ? ? ? if(i === objs.length-1) \
?? ? ? ? ? ? ? ? imgScr = imgScr + objs[i].src;\
?? ? ? ? ? ? ? ? else\
?? ? ? ? ? ? ? ? imgScr = imgScr + objs[i].src + '+';\
?? ? ? ? ? ? ? ? };\
?? ? ? ? ? ? ? ? return imgScr;\
?? ? ? ? ? ? ? ? };\
?? ? ? ? ? ? ? ? getImages();" ] ;
? ? self.imageArray = [NSMutableArray arrayWithArray:[object componentsSeparatedByString:@"+"]];
? ? //插入js代碼,獲取所有圖片
? ? id object1 = [webView stringByEvaluatingJavaScriptFromString:@"\
?? ? ? ? ? ? ? ? function getImages(){\
?? ? ? ? ? ? ? ? var objs = document.getElementsByTagName(\"img\");\
?? ? ? ? ? ? ? ? var imgScr = '';\
?? ? ? ? ? ? ? ? for(var i=0;i
?? ? ? ? ? ? ? ? if(i === objs.length-1) \
?? ? ? ? ? ? ? ? imgScr = imgScr + objs[i].clientWidth/objs[i].clientHeight;\
?? ? ? ? ? ? ? ? else\
?? ? ? ? ? ? ? ? imgScr = imgScr + objs[i].clientWidth/objs[i].clientHeight + '+';\
?? ? ? ? ? ? ? ? };\
?? ? ? ? ? ? ? ? return imgScr;\
?? ? ? ? ? ? ? ? };\
?? ? ? ? ? ? ? ? getImages();" ] ;
? ? self.WHArray = [NSMutableArray arrayWithArray:[object1 componentsSeparatedByString:@"+"]];
2.計算位置、顯示圖片瀏覽
?__weak typeof(self) weakSelf = self;
? ? self.context[@"getImage"] = ^(){
? ? ? ? NSArray*args = [JSContextcurrentArguments];
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
?? ? ? ? ? weakSelf.bigImage_url= [NSStringstringWithFormat:@"%@",args[0]];
? ? ? ? ? ? CGFloat y = [[NSStringstringWithFormat:@"%@",args[1]]floatValue]+self.commentariesTableView.y-self->consetY+Navigation_height;
? ? ? ? ? ? CGFloath = (WIDTH-30)/[[NSStringstringWithFormat:@"%@",args[2]]floatValue];
? ? ? ? ? ? weakSelf.bigImage_rect=CGRectMake(15, y,WIDTH-30, h);
? ? ? ? ? ? weakSelf.bigImageView= [[UIImageViewalloc]initWithFrame:weakSelf.bigImage_rect];
? ? ? ? ? ? [weakSelf.bigImageViewsd_setImageWithURL:[NSURLURLWithString:weakSelf.bigImage_url]];
? ? ? ? ? ? [WINDOWS_ViewaddSubview:weakSelf.bigImageView];
? ? ? ? ? ? CGFloath1 =WIDTH/[[NSStringstringWithFormat:@"%@",args[2]]floatValue];
? ? ? ? ? ? [UIView animateWithDuration:0.5 animations:^{
? ? ? ? ? ? ? ? weakSelf.bigImageView.frame=CGRectMake(0, (HEIGHT- h1)/2,WIDTH, h1);
? ? ? ? ? ? }completion:^(BOOLfinished) {
? ? ? ? ? ? ? ? weakSelf.view.userInteractionEnabled=YES;
? ? ? ? ? ? ? ? HZPhotoBrowser*browserVc = [[HZPhotoBrowseralloc]init];
? ? ? ? ? ? ? ? browserVc.imageCount= weakSelf.imageArray.count;// 圖片總數(shù)
? ? ? ? ? ? ? ? browserVc.currentImageIndex= [weakSelf.imageArrayindexOfObject:weakSelf.bigImage_url];//當(dāng)前點擊的圖片
? ? ? ? ? ? ? ? browserVc.delegate= weakSelf;
? ? ? ? ? ? ? ? [browserVcshow];
? ? ? ? ? ? }];
? ? ? ? });
? ? };
3.移除
//圖片瀏覽移除后 imageview消失移除
- (void)dismissViewControllerIndex:(NSInteger)tag {
? ? if (tag == [self.imageArray indexOfObject:self.bigImage_url]) {
? ? ? ? [UIView animateWithDuration:0.5 animations:^{
? ? ? ? ? ? self.bigImageView.frame = self.bigImage_rect;
? ? ? ? }completion:^(BOOLfinished) {
? ? ? ? ? ? [self.bigImageView removeFromSuperview];
? ? ? ? }];
? ? }else{
? ? ? ? [UIView animateWithDuration:0.5 animations:^{
? ? ? ? ? ? self.bigImageView.alpha=0;
? ? ? ? }completion:^(BOOLfinished) {
? ? ? ? ? ? [self.bigImageView removeFromSuperview];
? ? ? ? }];
? ? }
}
//圖片瀏覽滑動后imageview的位置修改
- (void)didScrollviewCurrentIndex:(NSInteger)index {
? ? CGFloatw =WIDTH;
? ? CGFloath = w/[self.WHArray[index]floatValue];
? ? CGFloatx =0;
? ? CGFloaty = (HEIGHT- h)/2;
? ? [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[index]]];
? ? self.bigImageView.frame=CGRectMake(x, y, w, h);
}