用Lua开发iphone程序

这两年来随着iphone,ipad在全球的热卖,用于开发ios程序的object-c语言也在编程语言榜上逐节攀升。不过用Object-C开发iphone程序还是不算方便,毕竟它只是一个面向对象的C,并没有什么自己的特性。C语言对于内存管理,业务逻辑的操作都不擅长,程序员们不能抛开这些“繁文缛节”而去专注业务的开发。

Lua脚本却能弥补Object-C语言的很多不足,除开著名的游戏*愤怒的小鸟*,还有很多其他ios程序游戏也是用了Lua去处理逻辑的工作(当然大名鼎鼎的魔兽世界也用了Lua做插件开发)。我最近在关注lua的应用时发现Wax这个框架,可以让我们基于它完全地用Lua进行IOS程序的开发。

我们来看看Wax的作者介绍为什么要用Lua开发iphone程序的:

  1. 自动的垃圾回收机制,不用再去费心处理alloc,retain,release(object c的内存分配释放函数);
  2. 只用写很少的代码,不用包含各种头文件,没有复杂的数据结构,Lua让你更简单地操作你的iphone;
  3. 提供了对UITouch,Cocoa,Foundation等框架的完全支持(这样就可以放心地忘掉Object-C了);
  4. 封装便捷简单的Http服务,方便地处理REST接口;
  5. Lua语言提供闭包机制,熟悉的人肯定觉得这是个很棒的语言特性;
  6. Lua内置一个强大的正则表达式库。

那我们来安装Wax框架试试,安装过程很简单,请参考项目wiki中的安装步骤,此处略

新建项目的时候不要选iOS中的Application,而是User Templates中的Wax项目,给工程命个名字,一行代码都不用写,直接运行模拟器


打开script目录下的lua代码,可以看到很简单的逻辑

[cpp]  view plain copy
  1. waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}  
  2.   
  3. -- Well done! You are almost ready to run a lua app on your iPhone!  
  4. --  
  5. -- Just run the app (??) in the simulator or on a device!   
  6. -- You will see a dull, bland "hello world" app (it is your job to spice it up.)  
  7. --  
  8. -- If your prefer using TextMate to edit the Lua code just type   
  9. -- 'rake tm' from the command line to setup a wax TextMate project.  
  10. --  
  11. -- What's next?  
  12. -- 1. Check out some of the example apps to learn how to do  
  13. --    more complicated apps.  
  14. -- 2. Then you start writing your app!  
  15. -- 3. Let us know if you need any help at http://groups.google.com/group/iphonewax  
  16.   
  17. function applicationDidFinishLaunching(self, application)  
  18.   local frame = UIScreen:mainScreen():bounds()  
  19.   self.window = UIWindow:initWithFrame(frame)  
  20.   self.window:setBackgroundColor(UIColor:colorWithRed_green_blue_alpha(0.196, 0.725, 0.702, 1))  
  21.   
  22.   local label = UILabel:initWithFrame(CGRect(0, 100, 320, 40))  
  23.   label:setFont(UIFont:boldSystemFontOfSize(30))  
  24.   label:setColor(UIColor:whiteColor())  
  25.   label:setBackgroundColor(UIColor:colorWithRed_green_blue_alpha(0.173, 0.651, 0.627, 1))  
  26.   label:setText("Hello Lua!")  
  27.   label:setTextAlignment(UITextAlignmentCenter)      
  28.   self.window:addSubview(label)  
  29.     
  30.   self.window:makeKeyAndVisible()  
  31.     
  32.   puts("")  
  33.   puts("-------------------------------------------------")  
  34.   puts("- You can print stuff to the console like this! -")  
  35.   puts("-------------------------------------------------")    
  36. end  
回想以前我尝试用Object-C创建控件时的那个费劲,直接放弃了。接着在script目录下创建一个lua文件MyTableViewController.lua
[cpp]  view plain copy
  1. waxClass{"MyTableViewController", UITableViewController}  
  2.         
  3. function init(self)  
  4.     self.super:initWithStyle(UITableViewStylePlain)  
  5.   
  6.      -- Here are the tableView's contents  
  7.     self.things = {"Planes""Trains""Automobiles"}  
  8.   
  9.     return self  
  10. end  
  11.         
  12. function numberOfSectionsInTableView(self, tableView)  
  13.     return 1  
  14. end  
  15.   
  16. function tableView_numberOfRowsInSection(self, tableView, section)  
  17.     return #self.things  
  18. end  
  19.   
  20. function tableView_cellForRowAtIndexPath(self, tableView, indexPath)  
  21.     local identifier = "MyTableViewControllerCell"  
  22.     local cell = tableView:dequeueReusableCellWithIdentifier(identifier) or  
  23.                  UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier)  
  24.   
  25.     local thing = self.things[indexPath:row() + 1] -- Must +1 because Lua arrays are 1 based  
  26.     cell:textLabel():setText(thing)  
  27.   
  28.     return cell  
  29. end  
接着修改AppDelegate.lua
[cpp]  view plain copy
  1. require "MyTableViewController"  
  2.   
  3. waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}  
  4.   
  5. function applicationDidFinishLaunching(self, application)  
  6.     local frame = UIScreen:mainScreen():bounds()  
  7.     self.window = UIWindow:initWithFrame(frame)  
  8.        
  9.     self.controller = MyTableViewController:init()  
  10.     self.window:addSubview(self.controller:view())  
  11.        
  12.     self.window:makeKeyAndVisible()  
  13. end  
运行程序,就可以看到一个列表了,就是我们手机中的Table控件



wax也支持嵌入Lua到传统方式创建的IOS应用程序中去,比如example中的IBExampleApp项目,已经用Interface Builder创建了一个黄色View以及一个蓝色View。

在界面Delegate加载完毕后调用init.lua脚本设置默认为蓝色view

[cpp]  view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.     [self.window makeKeyAndVisible];  
  3.     wax_start("init.lua", nil);  
  4.     return YES;  
  5. }  
init.lua脚本
[cpp]  view plain copy
  1. require "BlueController"  
  2. require "OrangeController"  
  3.   
  4. -- these are global just to make the code smaller... IT'S GENERALLY A BAD IDEA!  
  5. blueController = BlueController:init()  
  6. orangeController = OrangeController:init()  
  7.   
  8. local window = UIApplication:sharedApplication():keyWindow()  
  9. window:addSubview(blueController:view())  
然后分别在BlueController和OrangeController中定义Button的Click事件响应逻辑
[cpp]  view plain copy
  1. waxClass{"BlueController", UIViewController}  
  2.   
  3. IBOutlet "textField" -- This makes the property visible from IB  
  4.   
  5. function init(self)  
  6.   self.super:initWithNibName_bundle("BlueView", nil)  
  7.     
  8.   return self  
  9. end  
  10.   
  11. function viewDidLoad(self)  
  12.   -- The button and textField varibles are automatically created and added to the class via IB  
  13.   self.textField:setText("This text was created in Lua!")  
  14. end  
  15.   
  16. -- Put IBAction next to, or above a function to make it appear in IB  
  17. function buttonTouched(self, sender) -- IBAction  
  18.   local parentView = self:view():superview()  
  19.   UIView:beginAnimations_context(nil, nil)  
  20.   UIView:setAnimationTransition_forView_cache(UIViewAnimationTransitionFlipFromLeft, parentView, true)  
  21.   self:view():removeFromSuperview()  
  22.   parentView:addSubview(orangeController:view())  
  23.   UIView:commitAnimations()  
  24. end  


点击按钮能够实现界面的切换,我们通过上面的示例程序,可以看出lua代码中可以控制iphone创建UI控件,并且设置控件的行为,有这些功能应该足够应付很多日常的业务了。听说愤怒的小鸟只有一些底层的代码是用object-c实现的,上层的业务逻辑还是用Lua,通过我最近对Lua的尝试学习,感觉还是算一门比较容易上手的脚本语言。Wax中还有几个示例程序,由于我对IOS开发不熟悉,也就懒得看它的其他应用了。


[plain]  view plain copy
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值