IOS 游戏键盘

如何在运行在ios上的游戏创建键盘,接下来,就按下面的思路进行,有关这一块,本来做的有点久了,有些有点忘了。简单的思路,就是创建一个UITextField 对象,让当你游戏中点击你的UI控件时,实际上就触发UITextField键盘事件,整个游戏过程中,就一个这个对象。大体思路如下:

创建一个类专门来管理键盘,这个类进行对ios object-c类的处理,这样就可以简单快捷的处理ios键盘,命名为:KsIosKeyboard,在该类中我实现了:

方法:创建一个键盘、设置该键盘由你游戏中哪个对象控制、调整控制输入的内容、设置键盘类型

成员对象:保存ios 的键盘m_pIOSKey

void* KsIosKeyboard::createKeyBoard( void )

{

        KsIOSKeyboardCallBack* pCall = [[KsIOSKeyboardCallBackalloc] init];

        [pCall buildIOSKeyBoard];
         m_pIOSKey= (void*)pCall;
         return m_pIOSKey;

}

这个函数中的KsIOSKeyboardCallBack对象是专门对IOS键盘实现的类,一下可以看到。这个函数主要做,创建一个ios输入键盘,保存这个对象以便之后使用。


void KsIosKeyboard::destroy( void )
{
        if( m_pIOSKey!= NULL )
        {
            [m_pIOSKeydestroy];
            [m_pIOSKeyrelease];
        }
 }

//这个函数用于内存释放。

    
    void KsIosKeyboard::setControls(void *pControls)
    {
        KsIOSKeyboardCallBack* pCall = (
KsIOSKeyboardCallBack*)m_pIOSKey;
        if( pCall != NULL )
        {
            [pCall setControls:pControls];
        }
    }

// 设置你游戏中的输入UI控件。可以在你游戏中的UI控件出于焦点时调用。

    
    void KsIosKeyboard::adjustShowText(wstring strText, wstring strOlText, int maxChar)
    {
        KsIOSKeyboardCallBack* pCall = (KsIOSKeyboardCallBack*)m_pIOSKey;
        if( pCall )
        {
            [pCall adjustText:strText oldText:strOlText maxChar:maxChar];
        }
    }

// 这个函数用来调整你在游戏中输入控件的文本内容,比如你的输入内容有长度限制时
    
    void KsIosKeyboard::setKeyboardType( int type )
    {
        KsIOSKeyboardCallBack* pCall = (KsIOSKeyboardCallBack*)m_pIOSKey;
        if( pCall )
        {
            [pCall setKeyBoardType:type];
        }
    }

// 这个函数主要是设置键盘的类型。这个类型值与ios 键盘的类型对应。


接下来事情就创建一个ios 键盘的回调 KSIOSKeyboardCallBack,直接上代码了,不一一说了



@interface KsIOSKeyboardCallBack : NSObject
{
    void* pControls; // 这个是游戏中的UI控件
    bool  bAdjust; // 是否在输入是不挡住输入框
    
    UITextField*          m_pIOSInput; // 这个就是用来触发键盘的IOS控件
    UITextField*          m_pIOSShowBarText;  //这个是键盘上用来显示输入内容的
}


-(void)keyboardWillShow:(NSNotification*)aNotification;
-(void)keyboardWillHiden:(NSNotification*)aNotification;
- (void) setControls: (void*) _pControls;
@end


@implementation KsIOSKeyboardCallBack 


- (void) setControls: (void*) _pControls
{
    [m_pIOSInput resignFirstResponder];
    [m_pIOSShowBarText resignFirstResponder];
    m_pIOSInput.text = @"";
    m_pIOSShowBarText.text = @"";
    pControls = NULL;
    if( _pControls == NULL ) return;
    pControls = _pControls;
    [m_pIOSInput becomeFirstResponder];
}


- (void) destroy
{
    [m_pIOSInput release];
    [m_pIOSShowBarText release];
}


- (void) setKeyBoardType:(int)type
{
    if(m_pIOSShowBarText)
    {
        if( type == EKeyboardType_ASCII || type == EKeyboardType_Password )
            m_pIOSShowBarText.keyboardType = UIKeyboardTypeASCIICapable;
        else
            m_pIOSShowBarText.keyboardType = UIKeyboardTypeDefault;
        m_pIOSShowBarText.autocorrectionType = UITextAutocorrectionTypeNo;
    }
    
    if( m_pIOSInput )
    {
        if( type == EKeyboardType_ASCII || type == EKeyboardType_Password )
            m_pIOSInput.keyboardType = UIKeyboardTypeASCIICapable;
        else
            m_pIOSInput.keyboardType = UIKeyboardTypeDefault;
        m_pIOSInput.autocorrectionType = UITextAutocorrectionTypeNo;
    }
}


- (void) adjustText:(wstring)strText oldText:(wstring)strOldText maxChar:(int)nChars
{
    if( strText.length() > nChars )
    {
        NSString* strchange = [[NSString alloc] initWithBytes:strOldText.c_str() length:strOldText.size()*4 encoding:NSUTF32LittleEndianStringEncoding];
        if( !m_pIOSShowBarText.editing )
            m_pIOSShowBarText.text =strchange;
        if( !m_pIOSInput.editing )
            m_pIOSInput.text =strchange;
        return;
    }
    NSString* strchange = [[NSString alloc] initWithBytes:strText.c_str() length:strText.size()*4 encoding:NSUTF32LittleEndianStringEncoding];
    if( !m_pIOSShowBarText.editing )
        m_pIOSShowBarText.text =strchange;
    if( !m_pIOSInput.editing )
        m_pIOSInput.text =strchange;
}


- (void) buildIOSKeyBoard
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
    
    GLKViewController* ppnaviController = (GLKViewController*)[[UIApplication sharedApplication] keyWindow].rootViewController;
    
    m_pIOSInput= [[UITextField alloc] initWithFrame:CGRectMake(-1000, -1000,0, 0)];
    [m_pIOSInput addTarget:self action:@selector(editingEnd:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //[m_pIOSInput addTarget:id(m_pIOSKeyboard) action:@selector(editingBegin:) forControlEvents:UIControlEventEditingDidBegin];
    [m_pIOSInput addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    m_pIOSInput.backgroundColor = [UIColor clearColor];
    m_pIOSInput.textColor=[UIColor clearColor];
    m_pIOSInput.keyboardType = UIKeyboardTypeDefault;
    m_pIOSInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
    m_pIOSInput.returnKeyType = UIReturnKeyDone;
    
    UIToolbar* pToolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0,0,100,40)] autorelease];
 //   UIBarButtonItem* pComplete = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(editingEndDone:)] autorelease];
    
    
    float fWidth = ppnaviController.view.frame.size.height - 40;
    
    m_pIOSShowBarText = [[UITextField alloc] initWithFrame:CGRectMake(0,0,fWidth,30)];
    m_pIOSShowBarText.backgroundColor = [UIColor whiteColor];
    m_pIOSShowBarText.returnKeyType = UIReturnKeyDone;
    m_pIOSShowBarText.autocapitalizationType = UITextAutocapitalizationTypeNone;
    m_pIOSShowBarText.keyboardType = UIKeyboardTypeDefault;
    //m_pIOSShowBarText = UITextAlignmentLeft;
    m_pIOSShowBarText.borderStyle = UITextBorderStyleRoundedRect;
    [m_pIOSShowBarText addTarget:self action:@selector(editingEnd:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [m_pIOSShowBarText addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    [m_pIOSShowBarText addTarget:self action:@selector(editingChangeFocusToBarTex:) forControlEvents:UIControlEventEditingDidBegin];
    [m_pIOSShowBarText addTarget:self action:@selector(editingEnd:) forControlEvents:UIControlEventEditingDidEnd];
    
    UIBarButtonItem* pToolContent = [[[UIBarButtonItem alloc] initWithCustomView:m_pIOSShowBarText] autorelease];
    [pToolBar setItems:[NSArray arrayWithObjects:pToolContent,nil] animated:YES];
    m_pIOSInput.inputAccessoryView = pToolBar;
    
    [ppnaviController.view addSubview:m_pIOSInput];
}


-(void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* pUserInfo = [aNotification userInfo];
    NSValue* pValue = [pUserInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect rc = [pValue CGRectValue];
    
    if( NULL != pControls )
    {
        ((DskUITextBox*)pControls)->OnKeyBoardWillShow( rc.size.width, rc.size.height);
    }
}


-(void)keyboardWillHiden:(NSNotification*)aNotification
{
    if( NULL != pControls )
    {
        ((DskUITextBox*)pControls)->OnKeyBoardWillHide();
    }
}


- (void) editingEnd: (id) sender
{
    UITextField* pText = (UITextField*)sender;
    if( NULL != pText && NULL != pControls )
    {
        //call back
        const wchar_t* wstrText = (const wchar_t*)[pText.text cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        
        if( wstrText)
        {

   //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
        else
        {
            //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
        
        [pText resignFirstResponder];
    }
    [m_pIOSInput resignFirstResponder];
    [m_pIOSShowBarText resignFirstResponder];
}


- (void) editingEndDone: (id) sender
{
    if( NULL != m_pIOSShowBarText && NULL != pControls )
    {
        //call back
        const wchar_t* wstrText = (const wchar_t*)[m_pIOSShowBarText.text cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        if( wstrText)
        {
             //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
        else
        {
             //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
    }
    [m_pIOSInput resignFirstResponder];
    [m_pIOSShowBarText resignFirstResponder];
}


- (void) editingChanged: (id) sender
{
    UITextField* pText = (UITextField*)sender;
    if( NULL != pText && NULL != pControls )
    {
        //call back
        const wchar_t* wstrText = (const wchar_t*)[pText.text cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        if( wstrText)
        {
             //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
        else
        {
             //TODO: 将这个对象转成你游戏中的UI控件,调用你认为需要的。
        }
        
        if( m_pIOSInput.isEditing && !m_pIOSShowBarText.isEditing )
        {
            [m_pIOSInput resignFirstResponder];
            [m_pIOSShowBarText becomeFirstResponder];
        }
    }
}


- (void) editingChangeFocusToBarTex:(id)sender
{
    [m_pIOSInput resignFirstResponder];
    [m_pIOSShowBarText becomeFirstResponder];
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值