【问题标题】:How to use keyboard in OpenGL es app如何在 OpenGL es 应用程序中使用键盘
【发布时间】:2016-12-01 15:19:09
【问题描述】:

我有一个使用键盘的 OpenGL ES 应用程序。当触摸屏幕时,我可以使键盘在屏幕上弹出。如果我是正确的,每按一次键,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

应该被调用。但事实并非如此。该应用程序最初是一个纯 OpenGL Mac 游戏,我正在尝试制作一个 iOS 版本,所以我没有使用故事板。如果可能的话,我更喜欢以编程方式做所有事情。这是我的 ViewController.h 代码:

#import <GLKit/GLKit.h>
#import "KeyboardView.h"

@interface ViewController : GLKViewController {
    KeyboardView* keyBoard;
}
@end

ViewController.m 的相关部分:

- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
    NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

CGRect viewRect = CGRectMake(0, 0, 100, 100);
keyBoard = [[KeyboardView alloc] initWithFrame:viewRect];

[self setupGL];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.view addSubview:keyBoard];
    [keyBoard becomeFirstResponder];
}

KeyboardView.h:

#import <UIKit/UIKit.h>

@interface KeyboardView : UIView <UIKeyInput, UITextFieldDelegate> {
    UITextField *field;
}

KeyboardView.m:

#import "KeyboardView.h"
@implementation KeyboardView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    field = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
    }
    return self;
}

- (void)insertText:(NSString *)text {

}

- (void)deleteBackward {

}

- (BOOL)hasText {
    return YES;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSLog(@"text: %@", textField.text);
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] < 1) {
    return YES;
} else
    {
    textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
    [textField resignFirstResponder];
    return NO;
    }
}

@end

我需要能够在键盘处于活动状态时获取用户输入的每个字符。我承认,我有点困惑。我不确定我的方法是否正确,因此非常感谢您的帮助。

【问题讨论】:

  • 您应该将文本字段“field”作为子视图添加到超级视图:[self addSubView: field];
  • 将字段添加到超级视图,没有区别。顺便说一句,我希望隐藏文本字段。我只想从键盘上获取按键,然后使用这些字符来做其他事情。

标签: ios objective-c opengl-es keyboard glkview


【解决方案1】:

键盘文本输入不是通过UITextField's 捕获的

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

确实是通过UIKeyInput的抓拍的

- (void)insertText:(NSString *)text;

我正在使用软键盘在我的 GLKView (OpenGLES) 应用程序上捕获文本,我什至根本不需要任何 UITextField

供参考:

UIKeyInput Reference Doc

编辑:

你需要调用你的keyboardview的becomeFirstResponder来显示你的键盘并调用resignFirstResponder来隐藏它

您还需要覆盖canBecomeFirstResponder 并返回TRUE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多