UIWebview仿微信進(jìn)度條

一削饵、實(shí)現(xiàn):
myViewController.h :

@interface myViewController : UIViewController {
     BOOL theBool;
     //IBOutlet means you can place the progressView in Interface Builder and connect it to your code
     IBOutlet UIProgressView* myProgressView;
     NSTimer *myTimer;
}
@end

myViewController.m:

#import "myViewController.h"

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 仿微信進(jìn)度條
    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    myProgressView = [[UIProgressView alloc] initWithFrame:barFrame];
    myProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    myProgressView.progressTintColor = [UIColor colorWithRed:43.0/255.0 green:186.0/255.0  blue:0.0/255.0  alpha:1.0];
    [self.navigationController.navigationBar addSubview:myProgressView];
   
    // 設(shè)置網(wǎng)絡(luò)請(qǐng)求失敗情況頁(yè)面顯示   
    [self loadFailViewSetting];
    // 請(qǐng)求網(wǎng)絡(luò)
    [self reRequesrtUrl];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
  
    // 移除 progress view
    // because UINavigationBar is shared with other ViewControllers
    [myProgressView removeFromSuperview];  
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
     myProgressView.progress = 0;
     theBool = false;
     //0.01667 is roughly 1/60, so it will update at 60 FPS
     myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     theBool = true;
}
-(void)timerCallback {
    if (theBool) {
         if (myProgressView.progress >= 1) {
              myProgressView.hidden = true;
              [myTimer invalidate];
              myTimer = nil;
         }
         else {
              myProgressView.progress += 0.1;
         }
    }
    else {
         myProgressView.progress += 0.05;
         if (myProgressView.progress >= 0.95) {
              myProgressView.progress = 0.95;
             // 注:這里關(guān)閉時(shí)間計(jì)時(shí)器,不然如果網(wǎng)頁(yè)加載非常慢券腔,計(jì)時(shí)器會(huì)一直執(zhí)行胎源,因?yàn)樗⑿卤容^迅速,會(huì)導(dǎo)致頁(yè)面卡頓吉执。這里關(guān)閉,在網(wǎng)頁(yè)請(qǐng)求結(jié)束代理里面地来,再執(zhí)行隱藏進(jìn)度條動(dòng)畫
             [myTimer invalidate];
              myTimer = nil;
         }
    }
}
@end

二戳玫、原理:

It's difficult (if even possible), since you would have to track all resources loaded by the site, but …
I have one idea. It's more of a **trick** than a real solution, but I think even Apple uses this in Messages app :)

1. When you start loading the page, **begin an animation to 90%** of the progress (let's say with duration of 1.5 seconds, maybe be different for Wi-Fi, LTE, 3G, …).
2. When page loads in meantime, **quickly animate the progress to 100%**. Done!
3. If the page takes more time to load, the progress bar **will stop at 90% and will wait there**. Frustrating moment when the user watches slow spinning indicator in status bar! And then finally, the page finish loading and (as in bullet 2.) you play **quick animation to 100%**. Done!

I think we all know, that this is how Messages app works, since I don't believe sending SMS can be tracked with such accurate progress :)

大致意思,就是這個(gè)進(jìn)度條是個(gè)假象未斑,先進(jìn)度到90%咕宿,然后等待加載完畢,完畢后瞬間進(jìn)度到100%蜡秽。

仿微信進(jìn)度條府阀,暫時(shí)是這樣處理的。如您有更好的方法载城,歡迎給予指點(diǎn)肌似。

三、補(bǔ)充(2016.11.02):

iOS 8 之后可以使用WKWebView替換UIWebView,性能各方面有很大提高诉瓦。學(xué)習(xí)期間寫了個(gè)小demo,點(diǎn)擊下載 下載地址

參考鏈接:

  1. http://www.reibang.com/p/318e774d82fe
  2. http://www.reibang.com/p/6ba2507445e4
  3. https://github.com/wangyangcc/IMYWebView
  4. http://blog.csdn.net/y550918116j/article/details/49684127
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末睬澡,一起剝皮案震驚了整個(gè)濱河市固额,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌煞聪,老刑警劉巖斗躏,帶你破解...
    沈念sama閱讀 222,865評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異昔脯,居然都是意外死亡啄糙,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門云稚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)隧饼,“玉大人,你說(shuō)我怎么就攤上這事静陈⊙嘌悖” “怎么了?”我有些...
    開封第一講書人閱讀 169,631評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵鲸拥,是天一觀的道長(zhǎng)拐格。 經(jīng)常有香客問我,道長(zhǎng)刑赶,這世上最難降的妖魔是什么捏浊? 我笑而不...
    開封第一講書人閱讀 60,199評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮撞叨,結(jié)果婚禮上呛伴,老公的妹妹穿的比我還像新娘。我一直安慰自己谒所,他們只是感情好热康,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,196評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著劣领,像睡著了一般姐军。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上尖淘,一...
    開封第一講書人閱讀 52,793評(píng)論 1 314
  • 那天奕锌,我揣著相機(jī)與錄音,去河邊找鬼村生。 笑死惊暴,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的趁桃。 我是一名探鬼主播辽话,決...
    沈念sama閱讀 41,221評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼肄鸽,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了油啤?” 一聲冷哼從身側(cè)響起典徘,我...
    開封第一講書人閱讀 40,174評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎益咬,沒想到半個(gè)月后逮诲,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,699評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡幽告,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,770評(píng)論 3 343
  • 正文 我和宋清朗相戀三年梅鹦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冗锁。...
    茶點(diǎn)故事閱讀 40,918評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡齐唆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蒿讥,到底是詐尸還是另有隱情蝶念,我是刑警寧澤,帶...
    沈念sama閱讀 36,573評(píng)論 5 351
  • 正文 年R本政府宣布芋绸,位于F島的核電站媒殉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏摔敛。R本人自食惡果不足惜廷蓉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,255評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望马昙。 院中可真熱鬧桃犬,春花似錦、人聲如沸行楞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)子房。三九已至形用,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間证杭,已是汗流浹背田度。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留解愤,地道東北人镇饺。 一個(gè)月前我還...
    沈念sama閱讀 49,364評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像送讲,于是被迫代替她去往敵國(guó)和親奸笤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子惋啃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,926評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容