Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Forked from anonymous/gist:4639734
Last active December 17, 2015 18:29
Show Gist options
  • Save sebnozzi/5653898 to your computer and use it in GitHub Desktop.
Save sebnozzi/5653898 to your computer and use it in GitHub Desktop.
Scala (2.10) version of the basic iOS app example of RoboVM: http://www.robovm.org/docs.html#ios-example
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])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment