[openFrameworks] ofxiPhone で Cocoa UIKit にアクセスする方法

- 2013-06-04

前々から気になっていた ofxiPhone で Cocoa UIKit にアクセスする方法。

rootViewController になっている ofxiPhoneViewController を NavigationController に むりやりねじ込む方法をやってみた。

ofxiPhoneViewController は setup では早すぎてまだ生成されてないので、 draw の中でdispatch_once している。もっといい方法はないのかな...。

 

testApp.h#pragma once

#include "ofMain.h" #include "ofxiPhone.h" #include "ofxiPhoneExtras.h"

class testApp : public ofxiPhoneApp{

private:
    UIViewController *subViewController;
    UINavigationController *navigationController;

public:
    void setup();
    void update();
    void draw();
    void exit();

    void touchDown(ofTouchEventArgs & touch);
    void touchMoved(ofTouchEventArgs & touch);
    void touchUp(ofTouchEventArgs & touch);
    void touchDoubleTap(ofTouchEventArgs & touch);
    void touchCancelled(ofTouchEventArgs & touch);

    void lostFocus();
    void gotFocus();
    void gotMemoryWarning();
    void deviceOrientationChanged(int newOrientation);

}; testApp.mm

#include "testApp.h"

static dispatch_once_t onceToken;

//-------------------------------------------------------------- void testApp::setup(){ ofxAccelerometer.setup();

ofRegisterTouchEvents(this);

ofxiPhoneAlerts.addListener(this);

subViewController = [[UIViewController alloc] init];

subViewController.title = @"Second View Controller";

}

//-------------------------------------------------------------- void testApp::update(){

}

//-------------------------------------------------------------- void testApp::draw(){

dispatch_once(&onceToken, ^{
    UIWindow * window = ofxiPhoneGetUIWindow();

    UIViewController *firstViewController = ofxiPhoneGetViewController();

    firstViewController.title = @"First View Controller";

    window.rootViewController = subViewController;

    navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

    window.rootViewController = navigationController;

    dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1f * NSEC_PER_SEC);
    dispatch_after(delay, dispatch_get_main_queue(), ^{
        [[(ofxiPhoneViewController*)firstViewController glView] startAnimation]; // GLViewがstopしてしまうのでstartを少し遅れて呼ぶ
    });
});

ofSetColor(0, 0, 0);
ofDrawBitmapString("This is ofxiPhoneGLView.. \nTouch and Push viewController", 10, ofGetHeight() / 2 - 50 );

}

//-------------------------------------------------------------- void testApp::exit(){

}

//-------------------------------------------------------------- void testApp::touchDown(ofTouchEventArgs & touch){

}

//-------------------------------------------------------------- void testApp::touchMoved(ofTouchEventArgs & touch){

}

//-------------------------------------------------------------- void testApp::touchUp(ofTouchEventArgs & touch){

/*
touch window to push viewController..
*/

[navigationController pushViewController:subViewController animated:YES];

}

//-------------------------------------------------------------- void testApp::touchDoubleTap(ofTouchEventArgs & touch){

}

//-------------------------------------------------------------- void testApp::touchCancelled(ofTouchEventArgs & touch){

}

//-------------------------------------------------------------- void testApp::lostFocus(){

}

//-------------------------------------------------------------- void testApp::gotFocus(){

}

//-------------------------------------------------------------- void testApp::gotMemoryWarning(){

}

//-------------------------------------------------------------- void testApp::deviceOrientationChanged(int newOrientation){

}

ここに

https://github.com/moxuse/ofxiPhonePushViewControllerTest/ プロジェクトのソース上げています。 Second View Controller 以降をストーリーボードを使用するようになっています。