public
Last active — forked from /gist:4639734

Scala (2.10) version of the basic iOS app example of RoboVM: http://www.robovm.org/docs.html#ios-example

  • Download Gist
RoboTest.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
import org.robovm.cocoatouch.coregraphics._
import org.robovm.cocoatouch.foundation._
import org.robovm.cocoatouch.uikit._
 
 
 
object Utils {
import scala.language.implicitConversions
 
implicit class RichUIControl(control: UIControl) {
def onTouchUpInside(handler: (UIControl, UIEvent) => Unit) {
control.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener {
override def onTouchUpInside(c: UIControl, e: UIEvent) = handler(c,e)
})
}
def onTouchUpInside(handler: => Unit) { onTouchUpInside { (_,_) => handler }}
}
def autoReleasePool(block: => Unit) {
val pool = new NSAutoreleasePool()
block
pool.drain()
}
implicit def tuple4ToCGRect(tuple4: (Float,Float,Float,Float)) = {
val (x,y,w,h) = tuple4
new CGRect(x,y,w,h)
}
}
 
 
class RoboTest extends UIApplicationDelegate.Adapter {
 
var window: UIWindow = _
 
override def didFinishLaunching(application: UIApplication, launchOptions: NSDictionary[_ <: NSObject, _ <: NSObject]) = {
import Utils._
val label = new UILabel(50.0f, 221.0f, 220.0f, 37.0f)
label.setText("")
label.setTextAlignment(NSTextAlignment.Center)
val button = UIButton.fromType(UIButtonType.RoundedRect)
button.setFrame(50.0f, 121.0f, 220.0f, 37.0f)
button.setTitle("Click me!", UIControlState.Normal)
 
var clickCount = 0
button.onTouchUpInside { (_,event) =>
label.setText(event.getTimestamp.toString)
button.setTitle("Hello from scala #" + {clickCount = clickCount + 1; clickCount}, UIControlState.Normal);
}
 
window = new UIWindow(UIScreen.getMainScreen().getBounds())
window.setBackgroundColor(UIColor.lightGrayColor())
window.addSubview(button)
window.addSubview(label)
window.makeKeyAndVisible()
 
true
}
}
 
object RoboTest {
def main(args: Array[String]) {
Utils.autoReleasePool {
UIApplication.main(args, null, classOf[RoboTest])
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.