ios - Swift 3 中点击声音实现的自定义数字键盘

标签 ios swift swift3 uicollectionview custom-keyboard

我有使用 UICollectionView 制作的自定义数字键盘。我在 didSelectItemAt 方法中获取按下的键值。每当调用 didSelectItemAt 方法时,我都希望键盘发出声音。它应该取决于用户的设备设置。

我已经引用了 Apple 开发人员文档,但我无法实现它。医生说:

To enable a custom input or accessory view for input clicks, perform the following two steps: 1. Adopt the UIInputViewAudioFeedback protocol in your input view class. 2. Implement the enableInputClicksWhenVisible delegate method to return true.

我尝试了以下方式:

  1. 创建了 CustomUICollectionView

class CustomUICollectionView: UICollectionView, UIInputViewAudioFeedback {
    var enableInputClicksWhenVisible: Bool {
        return true
     }
 }

  1. 在显示数字键盘的 Controller 中声明变量 numberPad。并在 didSelectItemAt 方法中调用了 playInputClick。

@IBOutlet weak var numberPad: CustomUICollectionView!<br/>
 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    UIDevice.current.playInputClick()
 }

最佳答案

  1. 您需要从符合 UIInputViewAudioFeedback 的 View 类中调用 playInputClick
  2. 符合UIInputViewAudioFeedback 并调用playInputClick 的类需要是文本字段或 TextView 的事件inputView

重构您的代码以满足这些要求,点击将起作用。

关于ios - Swift 3 中点击声音实现的自定义数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50829513/

相关文章:

ios - 旋转后 MKAnnotationView 标注未正确放置

ios - UICollectionViewCell 计数总是返回 0

swift - 在 Swift 3 中仅修剪字符串末尾的尾随空格

ios - swift 3 : How to animate a view background color similar to a progress bar

ios - 应用程序标识符与 'lite' 和 'pro' 版本

ios - 核心数据 - 查询对多关系

Swift:如何在出现键盘时使 View Controller 滚动到文本字段

swift - 如何在 Swift 中处理字典的哈希冲突

ios - 我不断收到使用未解析的标识符错误

ios - 返回按钮显示图像痕迹