自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

lua in iOS App

移動開發(fā) iOS
其實(shí)很早我在參加一個沙龍的時候,就聽到了點(diǎn)評的同學(xué)在用lua做ab test,雖然那個時候我覺得我自己很牛逼了,但是其實(shí)還是啥都沒有聽懂,直到今天才回過神來仔細(xì)看了下這個東西。

起源

其實(shí)很早我在參加一個沙龍的時候,就聽到了點(diǎn)評的同學(xué)在用lua做ab test,雖然那個時候我覺得我自己很牛逼了,但是其實(shí)還是啥都沒有聽懂,直到今天才回過神來仔細(xì)看了下這個東西。

Lua(簡稱擼?。┰趇OS中的確被廣泛的使用著,在行業(yè)中***的莫過于魔獸世界(山口山)以及移動互聯(lián)網(wǎng)的憤怒的小鳥。

Lua在cocos2d以及iOS的應(yīng)用動態(tài)變化上面使用比較廣泛,下面我們用兩個例子來說明下。
框架

不得不說,***的莫過于wax和waxpatch,一個是能夠在iOS中使用lua語言編寫界面控件,一個是能夠動態(tài)更新。
wax

我們首先先要下載wax.framework,然后新建一個iOS app的project,將該模塊添加到我們的工程中去。

接著我們需要在 AppDelegate.h import #import

在AppDlegate的實(shí)現(xiàn)中增加

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  2.     // Override point for customization after application launch. 
  3.     wax_start("init.lua", nil); 
  4.     return YES; 

接著我們來增加這個 init.lua ,如下代碼,其實(shí)就如同ViewController頭文件定義一樣。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  2.     // Override point for customization after application launch. 
  3.     wax_start("init.lua", nil); 
  4.     return YES; 

有了頭文件之后我們也需要有實(shí)現(xiàn)吧,這個代碼可讀性就比oc來的高多了,但是需要在***行聲明在oc中的這個類名。

  1. waxClass{"ViewController", UIViewController} 
  2.   
  3. function viewDidLoad(self) 
  4. self.super:viewDidLoad(self) 
  5.   
  6. local label = UILabel:initWithFrame(CGRect(012032040)) 
  7. label:setColor(UIColor:blackColor()) 
  8. label:setText("Hello Wax!"
  9. label:setTextAlignment(UITextAlignmentCenter) 
  10. local font = UIFont:fontWithName_size("Helvetica-Bold",50
  11. label:setFont(font) 
  12. self:view():addSubview(label) 
  13. end 

先不要急著編譯,我們還需要更改一下編譯的方式以及確認(rèn)framework已經(jīng)被準(zhǔn)確導(dǎo)入

我們來看下效果:

 

waxpatch

waxpatch完全就是基于這個wax的框架之上去做的一個動態(tài)更新的組件了。我們來看下動態(tài)更新的流程。

***步:增加一個加載的協(xié)議

增加一個 ProtocolLoader.h ,其中添加需要去動態(tài)更新的組建名稱。

  1. #import < UIKit/UIKit.h> 
  2.   
  3. @interface ProtocolLoader : NSObject < UIApplicationDelegate, UIWebViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate, UISearchBarDelegate, UITextViewDelegate, UITabBarControllerDelegate> {} 
  4. @end 
  5.   
  6. @implementation ProtocolLoader 
  7. @end 

第二步:聲明需要加載的遠(yuǎn)程服務(wù)器地址,并且增加解壓縮的頭文件和實(shí)現(xiàn)

我在 AppDelegate.m 中先聲明了我遠(yuǎn)程更新庫的地址:

  1. #define WAX_PATCH_URL @"https://github.com/monkeytest15/waxDemo/raw/master/patch.zip" 

同時增加解壓縮實(shí)現(xiàn):

第三步:加載

當(dāng)然,我們都會理解為加載的邏輯是在 AppDelegate.m 中實(shí)現(xiàn)的,不過其實(shí)在該文件中只是調(diào)用了加載這個方法,具體的實(shí)現(xiàn)我在debug的過程發(fā)現(xiàn)在 wax.m 的文件中,核心代碼如下:

  1. // Load stdlib 
  2.     // --------------- 
  3.     #ifdef WAX_STDLIB  
  4.         // If the stdlib was autogenerated and included in the source, load 
  5.         char stdlib[] = WAX_STDLIB; 
  6.         size_t stdlibSize = sizeof(stdlib); 
  7.     #else 
  8.         char stdlib[] = "require 'wax'"
  9.         size_t stdlibSize = strlen(stdlib); 
  10.     #endif 
  11.   
  12.     if (luaL_loadbuffer(L, stdlib, stdlibSize, "loading wax stdlib") || lua_pcall(L, 0, LUA_MULTRET, 0)) { 
  13.         fprintf(stderr,"Error opening wax scripts: %s\n", lua_tostring(L,-1)); 
  14.     } 

加載之后就會動態(tài)的加載我們遠(yuǎn)程服務(wù)端的邏輯.
遠(yuǎn)程zip包

接著我們來看下遠(yuǎn)程服務(wù)端上都有什么,遠(yuǎn)程服務(wù)端可以自己定義zip包的名字以及內(nèi)容,但約定的內(nèi)容是必須有一個patch.lua文件以及其他的.lua的文件,patch.lua中是需要定義本次更新的View的主類名稱。比如 require "MainViewController"

而其他的類自然就是需要更新的邏輯,如:

  1. waxClass{"MainViewController", UITableViewController} 
  2.   
  3. function tableView_cellForRowAtIndexPath(self, tableView, indexPath) 
  4.     local cell = self:ORIGtableView_cellForRowAtIndexPath(tableView, indexPath) 
  5.     cell:textLabel():setText("" .. (20 - indexPath:row())) 
  6.     cell:detailTextLabel():setText("This is monkey"
  7.     cell:textLabel():setTextColor(UIColor:blueColor()) 
  8.     return cell 
  9. end 

動態(tài)效果

然后我們來看下我更新之后的效果吧:

原文鏈接:http://blog.sina.com.cn/s/blog_7022adbf0102vcg3.html

責(zé)任編輯:chenqingxiang 來源: MonkeyTest的博客
相關(guān)推薦

2013-12-08 20:32:32

WaxLua

2021-11-23 10:25:35

性能優(yōu)化iOS App 啟動優(yōu)化

2015-07-09 15:04:53

JSPatch動態(tài)更新ios app

2013-06-08 15:48:32

iOS App蘋果iOS開發(fā)者

2021-07-21 16:30:38

iOSAPP架構(gòu)

2018-09-12 21:25:15

iOSAppcrash

2017-12-25 14:59:47

APP架構(gòu)iOS協(xié)議

2013-05-17 10:19:17

2012-01-05 09:19:25

iOSApp應(yīng)用

2013-01-15 10:38:06

iOSAppAppCan

2013-11-21 10:36:31

iOS APP開發(fā)工具

2015-10-09 09:24:08

2013-06-14 10:34:34

iOS App蘋果iOS開發(fā)者

2020-11-26 19:19:22

WindowsAndroid微軟

2018-12-12 15:30:28

Google LensiOSAPP

2012-06-01 11:02:33

2017-08-31 11:08:53

iOS架構(gòu)ReSwift

2013-09-09 16:11:16

iOS應(yīng)用內(nèi)置付費(fèi)IAP總結(jié)

2014-07-17 10:06:02

Model-View-iOS App

2018-12-07 12:54:22

App美團(tuán)外賣iOS客戶端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號