KeyboardViewController

KeyboardViewController

我正在为我的应用程序构建自定义键盘扩展。当用户单击一个键时,将播放一个名为“Tock.mp3”的自定义音频。它与系统提示音不同。

我有4个Objective-C类和一个.xib文件。它们分别称为KeyboardViewController.h / .m,Keyboard.h / .m / .xib

我将在最后附加类的代码。
因此,我尝试了以下方法:

 NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                               pathForResource:@"Tock"
                                               ofType:@"mp3"]];
    AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [click setVolume:0.15f];
    [click play];

我的问题:我需要什么代码,我必须在哪里实现?

这是KeyboardViewController.h的代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface KeyboardViewController : UIInputViewController
@end

KeyboardViewController.m:
#import "KeyboardViewController.h"
#import "Keyboard.h"

@interface KeyboardViewController ()
@property (strong, nonatomic)Keyboard *keyboard;

@end

@implementation KeyboardViewController

- (void)updateViewConstraints {
    [super updateViewConstraints];

    // Add custom view sizing constraints here
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Perform custom UI setup here
    self.keyboard = [[[NSBundle mainBundle] loadNibNamed:@"Keyboard" owner:nil options:nil]objectAtIndex:0];
    [self addGesturesToKeyboard];
    self.inputView = self.keyboard;
    ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated
}

- (void)textWillChange:(id<UITextInput>)textInput {
    // The app is about to change the document's contents. Perform any preparation here.
}

- (void)textDidChange:(id<UITextInput>)textInput {
    // The app has just changed the document's contents, the document context has been updated.

    UIColor *textColor = nil;
    if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
        textColor = [UIColor whiteColor];
    } else {
        textColor = [UIColor blackColor];
    }
}
#pragma mark Keyboards
-(void)addGesturesToKeyboard
{
    [self.keyboard.backKey addTarget:self action:@selector(pressDeleteKey) forControlEvents:UIControlEventTouchUpInside];
    [self.keyboard.leerzeichenKey addTarget:self action:@selector(pressLeerzeichenKey) forControlEvents:UIControlEventTouchUpInside];
    [self.keyboard.enterKey addTarget:self action:@selector(pressEnterKey) forControlEvents:UIControlEventTouchUpInside];
    [self.keyboard.numbersKey addTarget:self action:@selector(pressNumbersKey) forControlEvents:UIControlEventTouchUpInside];
    //Change to next Keyboard
    [self.keyboard.globeKey addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];

    for (UIButton * key in self.keyboard.KeysArray) {
        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

    }

  }
-(void)pressDeleteKey
{
    [self.textDocumentProxy deleteBackward];
}
-(void)pressLeerzeichenKey
{
    [self.textDocumentProxy insertText:@" "];
}
-(void)pressEnterKey
{
    [self.textDocumentProxy insertText:@"\n"];
}
-(void)pressKey: (UIButton*) key
{
    [self.textDocumentProxy insertText:[[key currentTitle]lowercaseString]];
}
-(void)pressNumbersKey
{
}

@end

Keyboard.h:
#import <UIKit/UIKit.h>


@interface Keyboard : UIView


@property (strong,nonatomic)IBOutletCollection(UIButton)NSArray *KeysArray;
@property (weak, nonatomic) IBOutlet UIButton *upKey;
@property (weak, nonatomic) IBOutlet UIButton *numbersKey;
@property (weak, nonatomic) IBOutlet UIButton *globeKey;
@property (weak, nonatomic) IBOutlet UIButton *leerzeichenKey;
@property (weak, nonatomic) IBOutlet UIButton *enterKey;
@property (weak, nonatomic) IBOutlet UIButton *backKey;

@end

最后,我的Keyboard.m(不是很多!:D):
#import "Keyboard.h"

@implementation Keyboard
@end

最佳答案

KeyboardViewController.m 中,我将创建以下方法:

-(void)playSound
{
    NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Tock"ofType:@"mp3"]];
    AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [click setVolume:0.15f];
    [click play];
}

然后,我将在每个事件处理程序中调用此方法:
-(void)pressDeleteKey
{
    **[self playSound];**
    [self.textDocumentProxy deleteBackward];
}
-(void)pressLeerzeichenKey
{
    **[self playSound];**
    [self.textDocumentProxy insertText:@" "];
}
-(void)pressEnterKey
{
    **[self playSound];**
    [self.textDocumentProxy insertText:@"\n"];
}
-(void)pressKey: (UIButton*) key
{
    **[self playSound];**
    [self.textDocumentProxy insertText:[[key currentTitle]lowercaseString]];
}
-(void)pressNumbersKey
{
    **[self playSound];**
}

与其使用AVAudioPlayer播放声音,不如将声音文件转换为 .caf 文件。要播放 .caf 文件,请参见Easiest way to play a CAF sound on the iPhone?

希望这可以帮助!

07-24 16:12