This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
manual:adding_uikit_elements_on_top_of_sparrow [2014/08/06 13:41] – [Adding UIKit Elements on top of Sparrow] 178.219.160.178 | manual:adding_uikit_elements_on_top_of_sparrow [2014/08/25 08:32] (current) – Removed vandalism ... daniel | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Adding UIKit Elements on top of Sparrow ====== | ||
+ | Sparrow renders all its contents directly into the '' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Mission Objective ===== | ||
+ | |||
+ | In this tutorial, we will add a simple '' | ||
+ | |||
+ | While the sample itself does not look super-useful, | ||
+ | |||
+ | This is how the scaffold app looks like in its original state: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Adding the Switch ===== | ||
+ | |||
+ | The unmodified version of the Scaffold project plays a sound when you touch the bird. Let's say we want to show the UIKit switch instead. To do this, we add a new method call to the event handler. | ||
+ | |||
+ | <code objc> | ||
+ | - (void)onImageTouched: | ||
+ | { | ||
+ | NSSet *touches = [event touchesWithTarget: | ||
+ | if ([touches anyObject]) | ||
+ | { | ||
+ | [self showSwitch]; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code objc> | ||
+ | @implementation Game | ||
+ | { | ||
+ | SPSprite *_contents; | ||
+ | UISwitch *_switch; // <- Add the member variable here | ||
+ | } | ||
+ | |||
+ | - (void)showSwitch | ||
+ | { | ||
+ | if (!_switch) | ||
+ | { | ||
+ | _switch = [[UISwitch alloc] initWithFrame: | ||
+ | [_switch addTarget: | ||
+ | | ||
+ | |||
+ | [Sparrow.currentController.view addSubview: | ||
+ | } | ||
+ | } | ||
+ | |||
+ | - (void)onSwitchValueChanged | ||
+ | { | ||
+ | NSLog(@" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This is where it gets interesting! Creating the '' | ||
+ | |||
+ | But how do we get access to the basic UIKit view object? In Sparrow 2, you can access that object from anywhere in your code like this: | ||
+ | |||
+ | <code objc> | ||
+ | SPViewController *viewController = Sparrow.currentController; | ||
+ | UIView *view = currentController.view; | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Now let's start up the project and see what happens. After tapping the bird, the switch should appear, and Xcode should log 1s and 0s when its state changes. | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Updating the Bird ===== | ||
+ | |||
+ | To make it a little more interesting, | ||
+ | |||
+ | <code objc> | ||
+ | - (void)onSwitchValueChanged | ||
+ | { | ||
+ | NSLog(@" | ||
+ | | ||
+ | SPImage *bird = (SPImage *)[_contents childAtIndex: | ||
+ | bird.color = _switch.isOn ? SP_RED : SP_WHITE; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | (It would have been cleaner to save the bird object as a member variable instead of accessing the child at index ' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | That should be it! You successfully added an UIKit object on top of Sparrow and are using it to modify a Sparrow display object! Cool, isn't it? Now you can add any other UIKit element just like that. | ||
+ | |||
+ | ===== Universal Apps ===== | ||
+ | |||
+ | There' | ||
+ | |||
+ | For this reason, you have to multiply the coordinates of your UIKit objects by a certain factor; on the iPad (both retina and non-retina), | ||
+ | |||
+ | <note idea> | ||
+ | To find out the required multiplication factor at runtime, you can simply divide " | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | Also note that some standard interface elements (e.g. switches) have a fixed size and should not be scaled. You have to take that into account when you place those objects on the screen. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | //Continue to [[Dynamic Textures]]// |