Implement a keyboard click sound, lost

谁都会走 提交于 2019-12-11 04:48:35

问题


Everyone on this site has been very helpful on my schooling of iOS programming, but I've run into a brick wall with a very simple feature. I've found Apples documentation on implementing the factory keyboard click sound upon a button touch, but what Im not getting is the "creating a sub lass of UIView" part. I have a very basic calculator that I've built, the buttons are made of standard Round Rect's that I want to make the simple click sound. So Apple says:Adopting the UIInputViewAudioFeedback Protocol Perform the following three steps to adopt the UIInputViewAudioFeedback protocol:

In your Xcode project, create a subclass of the UIView class. In the header file, indicate that the subclass conforms to the UIInputViewAudioFeedback protocol, as follows: @interface KeyboardAccessoryView : UIView { }

Now I am using the standard UIViewController tied to a xib with all my buttons. Am i to: Create New File, name it as a subclass of UIView, JUST so i can implement this sound? That doesn't make sense to me, and if this is really amateur stuff I apologize, but I'm learning from many different places. Thanks in advance.


回答1:


You have to set your custom input view as the inputView property of the object that is supposed to be the first responder (For example, a UITextField instance), and the inputView(your custom input view) must conform to the UIInputViewAudioFeedback protocol. And to actually play a click sound: [[UIDevice currentDevice] playinputClick].

For example:

@interface MyCalculatorDisplay : UIView
// ...
@end

@interface MyCustomKeyboard : UIView <UIInputViewAudioFeedback>
// ...
@end

// Then, somewhere in your controller:

MyCustomKeyboard *keyboard = [MyCustomKeyboard new];

MyCalculatorDisplay *display = [MyCalculatorDisplay new];

display.inputView = keyboard;


来源:https://stackoverflow.com/questions/10509024/implement-a-keyboard-click-sound-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!