iOS仿今日頭條新聞詳情的圖片點擊瀏覽

效果

思路:

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);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容