【发布时间】:2017-01-04 23:03:35
【问题描述】:
我创建了 UICollectionViewController 的子类,用作 UITextView 中的自定义 inputAccessoryViewController。
https://developer.apple.com/reference/uikit/uiresponder/1621124-inputaccessoryviewcontroller
当您使用 playInputClick 在集合视图中点击一个单元格时,我想播放键盘点击声音。 https://developer.apple.com/reference/uikit/uidevice/1620050-playinputclick
我不知道如何让它在集合视图中工作。它适用于像这样的简单视图,使用 UITextView 的 inputAccessoryView 属性,但我不确定在集合视图控制器层次结构中子类化哪个视图以播放键盘点击声音。
@interface KeyboardClickView : UIView <UIInputViewAudioFeedback>
@end
@implementation KeyboardClickView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self addGestureRecognizer:tap];
}
return self;
}
- (void)tap:(id)sender
{
[[UIDevice currentDevice] playInputClick];
}
- (BOOL)enableInputClicksWhenVisible
{
return YES;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_inputAccessoryView = [[KeyboardClickView alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
_inputAccessoryView.backgroundColor = [UIColor redColor];
[[UITextView appearance] setInputAccessoryView:_inputAccessoryView];
// ...
}
@end
我也知道您可以使用AudioServicesPlaySystemSound(1104) 播放键盘点击声音,但如果他们禁用了键盘点击声音,这不尊重用户的设置。
【问题讨论】:
标签: ios objective-c uicollectionview