IOS中的webView加載HTML
在日常開(kāi)發(fā)中,我們?yōu)榱诵蕰?huì)用到很多很多的WebView,比如在做某個(gè)明細(xì)頁(yè)面的時(shí)候我們返回給你的可能是一個(gè)html字符串,我們就需要將當(dāng)前字符串展示到webView上面,所以我們對(duì)HTML標(biāo)簽需要有一定的認(rèn)識(shí),下面我們來(lái)一起用html標(biāo)簽和JS寫(xiě)一個(gè)打地鼠游戲,這里我們主要講解HTML標(biāo)簽的書(shū)寫(xiě),只要如何和webView適配涉及到響應(yīng)式布局我們下次講解:
1、首先我們先新建一個(gè)html文件
2 完整html標(biāo)簽并且設(shè)置編碼格式為UTF-8
3 在body里面增加十只老鼠圖片,并且增加點(diǎn)擊事件,當(dāng)點(diǎn)擊老鼠后觸發(fā)JS函數(shù)onclick="addScore(this);",代碼如下:
<img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:100px;top:200px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:200px;top:280px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:150px;top:100px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:300px;top:120px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:400px;top:200px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:600px;top:250px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:670px;top:100px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:490px;top:60px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:590px;top:30px;display:none' onclick="addScore(this);"/> <img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:650px;top:300px;display:none' onclick="addScore(this);"/>
4 先將所有的老鼠圖片放入數(shù)組中,然后開(kāi)啟定時(shí)器,每秒調(diào)用兩次該方法,并且隨機(jī)顯示八個(gè)老鼠圖片
//展示老鼠 function showMouse(){ //隱藏所有的老鼠 hideAll(); //控制點(diǎn)擊次數(shù) times++; //超過(guò)20此結(jié)束點(diǎn)擊 if(times>20){ window.clearInterval(timer); alert("游戲結(jié)束,得分"+score+"分"); return; } //獲取所有的老鼠 var imgs=document.getElementsByTagName("img"); //隨機(jī)的顯示八只老鼠 for(var i=0;i<8;i++){ var tem=Math.random()*10; tem= Math.round(tem); var node=imgs[tem] node.style.display=""; } } //隱藏所有老鼠 function hideAll(){ var imgs=document.getElementsByTagName("img"); for(var i=0 ;i<imgs.length;i++){ var tem=imgs[i]; tem.style.display="none"; } }
5 每次點(diǎn)擊我們需要隱藏當(dāng)前的圖片,并且增加分?jǐn)?shù),每點(diǎn)擊一次老鼠增加一分
//增加分?jǐn)?shù) function addScore(cell){ cell.style.display="none"; score++; document.getElementById("label").innerHTML=score+"分?jǐn)?shù)"; }
6 html加載到webView中顯示
UIWebView * web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)]; NSString * path=[[NSBundle mainBundle] pathForResource:@"mouse.html" ofType:nil]; NSData * data=[NSData dataWithContentsOfFile:path]; NSString * str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; web.scalesPageToFit=YES; [web loadHTMLString:str baseURL:nil]; [self.view addSubview:web];
以上所述是小編給大家介紹的IOS中的webView加載HTML的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- iOS開(kāi)發(fā)中WebView的基本使用方法簡(jiǎn)介
- IOS中UIWebView加載Loading的實(shí)現(xiàn)方法
- 輕松理解iOS 11中webview的視口
- 詳解iOS webview加載時(shí)序和緩存問(wèn)題總結(jié)
- iOS Webview自適應(yīng)實(shí)際內(nèi)容高度的4種方法詳解
- ios開(kāi)發(fā)加載webview顯示進(jìn)度條實(shí)例
- iOS 對(duì)當(dāng)前webView進(jìn)行截屏的方法
- iOS獲取cell中webview的內(nèi)容尺寸
- iOS去除Webview鍵盤(pán)頂部工具欄的方法
相關(guān)文章
iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能
這篇文章主要介紹了iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能,需要的朋友可以參考下2017-09-09iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法
這篇文章主要給大家介紹了關(guān)于iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07iOS開(kāi)發(fā)中UITableview控件的基本使用及性能優(yōu)化方法
這篇文章主要介紹了iOS開(kāi)發(fā)中UITableview控件的基本使用及性能優(yōu)化方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS 獲取當(dāng)前時(shí)間及時(shí)間戳的互換實(shí)例
下面小編就為大家分享一篇iOS 獲取當(dāng)前時(shí)間及時(shí)間戳的互換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01ios的手勢(shì)操作之UIGestureRecognizer淺析(推薦)
本篇文章主要介紹了ios的手勢(shì)操作之UIGestureRecognizer淺析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12