glkview drawinrect委托方法仅调用一次 [英] glkview drawinrect delegate method called only once

查看:112
本文介绍了glkview drawinrect委托方法仅调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将GLKViewController添加到我的UserInterfaceViewController中.我正在使用raywenderlich教程中的示例项目(

上面的代码是在我期望的上述框架中的UserInterfaceViewController视图中绘制上下文.HelloGLKitViewController具有GLKViewControllerDelegate方法更新"中的多维数据集,如下所示.我的UserInterfaceViewController视图.

#import <GLKit/GLKit.h>
@interface HelloGLKitViewController : GLKViewController

@end

@implementation HelloGLKitViewController 

#pragma mark - GLKViewDelegate

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    //glClearColor(_curRed, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);


    [self.effect prepareToDraw];

    glBindVertexArrayOES(_vertexArray);   
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

}

#pragma mark - GLKViewControllerDelegate

- (void)update {
    if (_increasing) {
        _curRed += 1.0 * self.timeSinceLastUpdate;
    } else {
        _curRed -= 1.0 * self.timeSinceLastUpdate;
    }
    if (_curRed >= 1.0) {
        _curRed = 1.0;
        _increasing = NO;
    }
    if (_curRed <= 0.0) {
        _curRed = 0.0;
        _increasing = YES;
    }

    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);    
    self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
    _rotation += -90 * self.timeSinceLastUpdate;//90 clockwise -90 anticlickwise
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(0), 1, 0, 0);//GLKMathDegreesToRadians(25) for bending Cube
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_rotation), 0, 1, 0);

//    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, self.view.frame.origin.x, self.view.frame.origin.y,0);
 //   modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -self.view.bounds.size.width/2, -self.view.bounds.size.height/2,0);

    self.effect.transform.modelviewMatrix = modelViewMatrix;

}

@end

我该怎么做才能调用GLKViewControllerDelegate更新方法.

问题已解决:这是我的代码

@class HelloGLKitViewController;

@interface UserInterfaceViewController : UIViewController
{
    HelloGLKitViewController *myHLKViewController;
}

@end

@implementation UserInterfaceViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    NSLog(@"viewDidLoad of userinterfaceUiViewController");

    myHLKViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"HelloGLKitViewController"];

    [self.view addSubview:myHLKViewController.view];
    [self addChildViewController:myHLKViewController];
    [myHLKViewController didMoveToParentViewController:self];
}

@end

感谢ref链接将GLKView与UIViewController一起使用

I am trying to add GLKViewController to my UserInterfaceViewController.I am using the example project from raywenderlich tutorials(http://www.raywenderlich.com/5235/beginning-opengl-es-2-0-with-glkit-part-2)

Here is my code snippet

#import <UIKit/UIKit.h>
@interface UserInterfaceViewController : UIViewController

@end

@implementation UserInterfaceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    UIViewController *userInterfaceController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"HelloGLKitViewController"];

    userInterfaceController.view.frame = CGRectMake(50.0, 100.0, 200.0, 200.0);

    [self.view addSubview:userInterfaceController.view];
}

The above code is drawing the context in my UserInterfaceViewController view in the mentioned frame which I am expecting.HelloGLKitViewController is having code to rotate cube in GLKViewControllerDelegate method "update" as mntioned below.but GLKViewControllerDelegate is not getting called when HelloGLKitViewController view to my UserInterfaceViewController view.

#import <GLKit/GLKit.h>
@interface HelloGLKitViewController : GLKViewController

@end

@implementation HelloGLKitViewController 

#pragma mark - GLKViewDelegate

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    //glClearColor(_curRed, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);


    [self.effect prepareToDraw];

    glBindVertexArrayOES(_vertexArray);   
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

}

#pragma mark - GLKViewControllerDelegate

- (void)update {
    if (_increasing) {
        _curRed += 1.0 * self.timeSinceLastUpdate;
    } else {
        _curRed -= 1.0 * self.timeSinceLastUpdate;
    }
    if (_curRed >= 1.0) {
        _curRed = 1.0;
        _increasing = NO;
    }
    if (_curRed <= 0.0) {
        _curRed = 0.0;
        _increasing = YES;
    }

    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);    
    self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
    _rotation += -90 * self.timeSinceLastUpdate;//90 clockwise -90 anticlickwise
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(0), 1, 0, 0);//GLKMathDegreesToRadians(25) for bending Cube
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_rotation), 0, 1, 0);

//    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, self.view.frame.origin.x, self.view.frame.origin.y,0);
 //   modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -self.view.bounds.size.width/2, -self.view.bounds.size.height/2,0);

    self.effect.transform.modelviewMatrix = modelViewMatrix;

}

@end

What should I do to call GLKViewControllerDelegate update method.

解决方案

Problem solved: Here is my code

@class HelloGLKitViewController;

@interface UserInterfaceViewController : UIViewController
{
    HelloGLKitViewController *myHLKViewController;
}

@end

@implementation UserInterfaceViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    NSLog(@"viewDidLoad of userinterfaceUiViewController");

    myHLKViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"HelloGLKitViewController"];

    [self.view addSubview:myHLKViewController.view];
    [self addChildViewController:myHLKViewController];
    [myHLKViewController didMoveToParentViewController:self];
}

@end

Thanks for the ref link Using GLKView with a UIViewController

这篇关于glkview drawinrect委托方法仅调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆