【OF/iOS】Customize AppDelegate.

・Viewを重ねる方法

・OFのViewの背景を透過する。

    ofxiOSSetGLViewTransparent(true);

 

main.m

#include "ofMain.h"
#include "ofApp.h"

int main(){
    
    ofAppiOSWindow * window = new ofAppiOSWindow();
    window->enableRetina();
    
    ofSetupOpenGL(ofPtr<ofAppBaseWindow>(window), 1024,768, OF_FULLSCREEN);
    window->startAppWithDelegate("MyAppDelegate");
}

 

MyAppDelegate.mm

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize vc;
AVCaptureVideoPreviewLayer* previewLayer;
UIView* cameraView;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [super applicationDidFinishLaunching:application];
    
    self.vc = [[RootViewController alloc] init];
    [self.window setRootViewController: self.vc];
    
    setupCameraPreview();
    [self.vc.view addSubview: cameraView];

    AppViewController* app = [[AppViewController alloc] init];
    [self.vc.view addSubview: app.view];

    
    return YES;
}

- (void) dealloc {
    self.vc = nil;
    [super dealloc];
}



void setupCameraPreview()
{
    AVCaptureSession *session = [AVCaptureSession new];
//    [session setSessionPreset:AVCaptureSessionPreset1920x1080];
    [session setSessionPreset:AVCaptureSessionPreset1280x720];
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    if ([session canAddInput:deviceInput]){
        [session addInput:deviceInput];
    }
    
    previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    
    cameraView = [[UIView alloc]initWithFrame: CGRectMake(0,0,640/2,1136/2)];
    cameraView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.f];
    
    CALayer *rootLayer = [cameraView layer];
    [rootLayer setMasksToBounds:YES];
    [previewLayer setFrame:[rootLayer bounds]];
    [rootLayer addSublayer:previewLayer];
    [session startRunning];
   
}
@end

 

MyAppDelegate.h

#import "ofxiOSAppDelegate.h"

#import <AVFoundation/AVFoundation.h>
#import "AppViewController.h"
#import "RootViewController.h"

@interface MyAppDelegate : ofxiOSAppDelegate {

}

@property (nonatomic, retain) RootViewController* vc;

 

RootViewController.h

@interface RootViewController : UIViewController

@end

 

RootViewController.mm

#import <Foundation/Foundation.h>
#import "RootViewController.h"

@implementation RootViewController

- (BOOL)shouldAutorotate {

    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}
@end

 

AppViewController.mm

#import "AppViewController.h"
#import "ofApp.h"

@implementation AppViewController

- (id)init {

   self = [super initWithFrame:[UIScreen mainScreen].bounds app: new ofApp()];
    if(self != nil) {

    }
    return self;
}

@end

 

AppViewController.h

#import "ofxiOSViewController.h"

@interface AppViewController : ofxiOSViewController

@end

ofApp.mm

//--------------------------------------------------------------
void ofApp::setup(){
 
    ofBackground(0,0,0,0);
    ofSetOrientation(OF_ORIENTATION_DEFAULT);
    
    ofxiOSSendGLViewToFront();
    ofxiOSSetGLViewTransparent(true);
//    ofxiOSSetGLViewUserInteraction(false);
    
    ofEnableAlphaBlending();
    camera.setPosition(0,0,0);
    camera.setupPerspective();
    camera.setVFlip(false);
    camera.setFarClip(5000);
    //camera.disableMouseInput();
}


//--------------------------------------------------------------
void ofApp::update() {
}

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

    camera.begin();

    ofSetColor(255, 0, 0,100);
    ofSetConeResolution(32, 16);
    ofDrawCone(0, 0, 0, 25, 50);

    camera.end();
}

//--------------------------------------------------------------
void ofApp::exit(){
    
}

ofApp.h

class ofApp : public ofxiOSApp {

    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);

}

 

 

 

 

 

Leave a Reply