UICollectionViewCell
( )のサブクラスがあります。これには、押されたときに音を鳴らしたいCustomCell
単一のUIButton
( ) があります。button
特に、変数が になったときにキーボードの文字の音が再生され、変数isOn
が になったときtrue
にキーボードのバックスペース (または削除) の音が再生されるようにisOn
しますfalse
。
これまでのところ、次のものがあります。
class CustomCell: UICollectionViewCell {
private var isOn = true
@IBOutlet weak private var button: UIButton! {
didSet {
button.addTarget(self, action: #selector(self.toggleButton), for: .touchUpInside)
}
}
@objc private func toggleButton() {
if (isOn) {
/// Play keyboard backspace (delete) sound ...
UIDevice.current.playInputClick()
} else {
/// Play keyboard text sound ...
UIDevice.current.playInputClick()
}
isOn = !isOn
}
}
また、次のようにプロトコルを実装しUIInputViewAudioFeedback
ます。
extension CustomCell: UIInputViewAudioFeedback {
func enableInputClicksWhenVisible() -> Bool {
return true
}
}
ただし、ボタンを押しても音は出ません。
助けてくれてありがとう。