IOS中的webView加載HTML
在日常開發(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寫一個(gè)打地鼠游戲,這里我們主要講解HTML標(biāo)簽的書寫,只要如何和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ù)組中,然后開啟定時(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ì)大家有所幫助。
相關(guān)文章
IOS 開發(fā)之UILabel 或者 UIButton加下劃線鏈接
這篇文章主要介紹了IOS 開發(fā)之UILabel 或者 UIButton加下劃線鏈接的相關(guān)資料,需要的朋友可以參考下2017-07-07
iOS 實(shí)現(xiàn)多代理的方法及實(shí)例代碼
這篇文章主要介紹了iOS 實(shí)現(xiàn)多代理的方法及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
設(shè)計(jì)模式開發(fā)中的備忘錄模式在iOS應(yīng)用開發(fā)中的運(yùn)用實(shí)例
這篇文章主要介紹了設(shè)計(jì)模式開發(fā)中的備忘錄模式在iOS應(yīng)用開發(fā)中的實(shí)例,代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
Objective-C中利用正則去除非數(shù)字字母漢字方法實(shí)例
正則表達(dá)式對(duì)我們?nèi)粘i_發(fā)來(lái)說(shuō)是必不可少的,下面這篇文章主要給大家介紹了關(guān)于Objective-C中如何利用正則去除非數(shù)字字母漢字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
iOS 12+ 中檢測(cè)網(wǎng)絡(luò)訪問(wèn)的方法
這篇文章主要介紹了iOS 12+ 中檢測(cè)網(wǎng)絡(luò)訪問(wèn)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
iOS 16 CocoaAsyncSocket 崩潰修復(fù)詳解
這篇文章主要為大家介紹了iOS 16 CocoaAsyncSocket 崩潰修復(fù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

