Skip to content

Instantly share code, notes, and snippets.

@magnatronus
Created July 28, 2017 11:40
Show Gist options
  • Save magnatronus/9ca2a0a068c625d8c53ba42d5aa862ff to your computer and use it in GitHub Desktop.
Save magnatronus/9ca2a0a068c625d8c53ba42d5aa862ff to your computer and use it in GitHub Desktop.
Titanium iOS Hyperloop example using Erbium and ES6
/**
* Testing Hyperloop in Erbium ES6 franework
*
* This uses the Hyperloop example from the Axway blog post
* https://www.appcelerator.com/blog/2016/08/hyperloop-working-with-native-controls-in-ios/
*
* But it has been updated to use ES6
* 28 July 2017
*/
import UIButton from 'UIKit/UIButton';
import { UIControlStateNormal, UIControlStateHighlighted, UIControlEventTouchUpInside } from 'UIKit';
import { blueColor } from 'UIKit/UIColor';
import { CGRectMake } from 'CoreGraphics';
import { Component } from '/system/erbium';
// Our Test Class
class App extends Component {
// Generate Our UI
generateView() {
// create the button
const button = new UIButton();
button.backgroundColor = blueColor;
button.layer.cornerRadius = 6;
button.frame = CGRectMake(50, 50, 300, 100);
button.setTitleForState('Click Me!',UIControlStateNormal);
button.setTitleForState('Highlighted!',UIControlStateHighlighted);
// create the delegate object (custom Hyperloop class)
const ButtonDelegate = Hyperloop.defineClass('ButtonDelegate', 'NSObject');
ButtonDelegate.addMethod({
selector: 'buttonPressed:',
instance: true,
arguments: ['UIButton'],
callback: function(sender) {
if (this.buttonPressed) {
this.buttonPressed(sender);
}
}
});
// create a delegate instance
const delegate = new ButtonDelegate();
delegate.buttonPressed = (sender) => {
alert('button pressed!');
};
// tie delegate to button
button.addTargetActionForControlEvents(delegate, 'buttonPressed:', UIControlEventTouchUpInside);
// create main window
const mainWindow = Ti.UI.createWindow({
backgroundColor: '#008080'
});
// add our Hyperloop button
mainWindow.add(button);
// return the UI
return mainWindow;
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment