如何在iPhone的OpenGLES应用程序中使用手势识别器?

4

我知道怎么在基于视图的应用程序中使用手势识别,但当我在一个基于OpenGLSE的应用程序中应用相同的想法时,例如添加了一个TapGestureRecognizer,当我点击EAGLView时,它会崩溃。所以有人能展示一下在基于OpenGLES的应用程序中使用UITapGestureRecognizer的标准用法吗?

最好的祝愿。


我个人没有直接经验,但你尝试将其添加到父视图(例如窗口)中了吗?不确定为什么,但它可能需要一些层功能,这些功能在CAEAGLLayer类中未实现。 - jhabbott
这非常奇怪,因为我已经让OpenGL ES托管视图响应正常的触摸事件而没有任何问题。为什么手势识别器会有所不同呢? - Brad Larson
1
我已经为基本的UIView和EAGLView使用了轻拍手势识别器,它们的工作方式完全相同。你可能在其他地方有问题。你的崩溃日志显示什么? - No one in particular
1个回答

4

以下是我其中一款带有手势支持的OpenGLES游戏的示例代码。(不会崩溃,希望对您有所帮助)

- (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]];      
}

选择器函数

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接