Created
March 24, 2011 10:53
-
-
Save jazzbox/884875 to your computer and use it in GitHub Desktop.
MacRuby: UIKit framework for Mac OS X (http://chameleonproject.org/) (https://github.com/BigZaphod/Chameleon/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/macruby | |
# This example is done with MacRuby and the UIKit framework for Mac OS X | |
# http://chameleonproject.org/ | |
# https://github.com/BigZaphod/Chameleon/ | |
framework 'Cocoa' | |
framework "UIKit" # garbage collected version, the current version of Chameleon ist not known to be GC-safe | |
################### | |
# generating the BridgeSupport file: | |
# mkdir -p /Library/Frameworks/UIKit.framework/Resources/BridgeSupport | |
# gen_bridge_metadata -f /Library/Frameworks/UIKit.framework -o /Library/Frameworks/UIKit.framework/Resources/BridgeSupport/UIKit.bridgesupport | |
################### | |
class AppDelegate | |
def initialize(v) | |
@view = v | |
end | |
def applicationDidFinishLaunching(notification) | |
chameleonApp = ChameleonAppDelegate.new | |
@view.launchApplicationWithDelegate(chameleonApp, afterDelay:0.5) | |
end | |
end | |
################### | |
class ChameleonAppDelegate | |
def applicationDidFinishLaunching(application) | |
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame) | |
@window.backgroundColor = UIColor.whiteColor | |
@window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | |
ns_img = NSImage.alloc.initWithContentsOfFile "apple.png" | |
ns_img ||= NSImage.imageNamed( NSImageNameTrashFull) | |
@imageRect = CGRectMake(0,0,ns_img.size.width,ns_img.size.height) | |
@imageView = UIImageView.alloc.initWithImage( UIImage.imageWithData(ns_img.TIFFRepresentation)) | |
@window.addSubview(@imageView) | |
@window.addSubview(UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |b| | |
b.setTitle("Click Me!", forState:UIControlStateNormal) | |
b.setTitleColor(UIColor.whiteColor, forState:UIControlStateNormal) | |
b.addTarget(self, action:'moveTheImage:', forControlEvents:UIControlEventTouchUpInside) | |
b.frame = CGRectMake(22,300,200,50) | |
end) | |
@window.makeKeyAndVisible | |
end | |
def moveTheImage(sender,event) | |
UIView.beginAnimations("moveTheImage", context:nil) | |
UIView.setAnimationDuration(3) | |
UIView.setAnimationBeginsFromCurrentState(true) | |
if (CGAffineTransformIsIdentity(@imageView.transform)) | |
@imageView.transform = CGAffineTransformMakeScale(2,2) | |
@imageView.center = @window.convertPoint(@window.center, toView:@imageView.superview) | |
else | |
@imageView.transform = CGAffineTransformIdentity | |
@imageView.frame = @imageRect | |
end | |
UIView.commitAnimations | |
end | |
end | |
################### | |
NSApplication.sharedApplication | |
NSApp.setActivationPolicy NSApplicationActivationPolicyRegular | |
NSApp.setMainMenu(NSMenu.new.tap do |menu| | |
menu.addItem(NSMenuItem.new.tap do |menu_item| | |
menu_item.setSubmenu(NSMenu.new.tap do |sub_menu| | |
sub_menu.addItem NSMenuItem.alloc. | |
initWithTitle "Quit #{NSProcessInfo.processInfo.processName}", | |
action: 'terminate:', | |
keyEquivalent: 'q' | |
end) | |
end) | |
end) | |
view = UIKitView.alloc.init | |
NSWindow.alloc.initWithContentRect( | |
[200,200,500,500], | |
styleMask:NSTitledWindowMask, | |
backing: NSBackingStoreBuffered, | |
defer: false | |
).tap do |win| | |
win.title = "Macruby UIKit Demo" | |
win.orderFrontRegardless | |
win.setContentView view | |
end | |
NSApp.activateIgnoringOtherApps true | |
NSApp.setDelegate(AppDelegate.new(view)) | |
NSApp.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment