如何在键盘上添加自定义按钮?

时间:2012-09-17 21:33:56

标签: ios xcode uikeyboard

我想在我的应用程序中使用这样的键盘:

Google Chrome App

如何实现这一目标? 如何使用几个突击队列添加行?

2 个答案:

答案 0 :(得分:1)

通过为inputAccessoryView实施UIResponder添加按钮。

答案 1 :(得分:1)

我所做的是从头开始创建一个键盘。在底部的Fist视图中使用两个UIViews和按钮。在这些按钮上禁用用户交互,然后在此处使用此代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:keyboardView];
    //here loops all labels
    for(UIButton *keyButton in keyboardView.subviews){
        if (CGRectContainsPoint(keyButton.frame,touchPoint)&&keyButton.enabled) {
            keyButton.highlighted = YES;
            for(UIButton *bigKey in keyboardTextView.subviews){
                if (bigKey.currentTitle==keyButton.currentTitle) {
                    bigKey.hidden=NO;
                }
                else{
                    bigKey.hidden=YES;
                }
            }
        }
        else if(!CGRectContainsPoint(keyButton.frame,touchPoint)){
            keyButton.highlighted = NO;
        }
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:keyboardView];
    //here loops all labels
    for(UIButton *keyButton in keyboardView.subviews){
        if (CGRectContainsPoint(keyButton.frame,touchPoint)&&keyButton.enabled) {
            keyButton.highlighted = YES;
            for(UIButton *bigKey in keyboardTextView.subviews){
                if (bigKey.currentTitle==keyButton.currentTitle) {
                    bigKey.hidden=NO;
                }
                else{
                    bigKey.hidden=YES;
                }
            }
        }
        else if(!CGRectContainsPoint(keyButton.frame,touchPoint)){
            keyButton.highlighted = NO;
        }
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:keyboardView];
    for(UIButton *keyButton in keyboardView.subviews){
        keyButton.highlighted = NO;
        if(CGRectContainsPoint(keyButton.frame,touchPoint) && keyButton.enabled == YES){
            keyButton.enabled = NO;
            letterUsedString=keyButton.currentTitle;
            // right here you can use the letter Used String.
        }
    }
    for(UIButton *bigKey in keyboardTextView.subviews){
        bigKey.hidden=YES;
    }
}

您只需要两个IBOutlets视图,keyboardTextView和键盘视图。我会上传一些文件,以便您可以看到我的键盘。所以第一个视图是你按下的按钮,一个按钮背景中的图像。第二个视图是突出显示的按钮。如果您实现该代码并连接两个IBOutlets,它将像键盘一样工作。任何无法识别的变量都会添加到.h。当按下我的按钮时,我突出显示了它们改变了图像:

this

到:

this

然后我在图像上方的放大按钮将是:

this.

只需将按钮放在您想要的位置,然后将放大按钮放在上面。放大的按钮视图可见,但内部的所有按钮都被隐藏,直到您按下。我希望我的代码有所帮助,你必须自己做故事板。