Search code examples
iphoneopengl-esios4eaglview

How to use gesture recognizer in an OpenGLES application of iPhone?


While I know how to use gesture recognizer in a view-based application,but when I apply the same ideas in a OpenGLSE-based application: for example, I add a TapGestureRecognizer,and when I tap on the EAGLView,it crashes. So can anyone show me a standard usage of UITapGestureRecognizer in an OpenGLES-based application?

best wishes.


Solution

  • Here some sample code from one of my opengles games with gesture support. (Doesn't crash and hope it helps)

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        CGRect  rect = [[UIScreen mainScreen] bounds];
        rect.size.height = 320;
        rect.size.width = 480;
        rect.origin.x = 0;
        rect.origin.y = 0;
    
        glView = [[EAGLView alloc] initWithFrame:rect pixelFormat:GL_RGB565_OES depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO];
        [self.view addSubview: glView];
    
        [glView addSubview: minimapView];
    
        if(!shell->InitApplication())
            printf("InitApplication error\n");
    
        [NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:@selector(update) userInfo:nil repeats:YES];
    
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(Panned:)];
        [glView addGestureRecognizer:[pan autorelease]];    
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tapped:)];
        [glView addGestureRecognizer:[tap autorelease]];    
    
        UITapGestureRecognizer *dbltap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTapped:)];
        [dbltap setNumberOfTapsRequired:2];
        [glView addGestureRecognizer:[dbltap autorelease]];
    
        UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressed:)];
        [glView addGestureRecognizer:[longpress autorelease]];      
    }
    

    And the selector function

    - (void) LongPressed:(UILongPressGestureRecognizer*)sender{
        NSLog(@"Long Pressed");
    }