I have a subclass of UICollectionViewCell (CustomCell), which has a single UIButton (button) that I want to play a sound when it is pressed. In particular, I want the keyboard letter sounds to play when the variable isOn turns to true and the keyboard backspace (or delete) sound to play when variable isOn turns to false.
So far I have the following:
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
}
}
I also implement the UIInputViewAudioFeedback protocol as follows:
extension CustomCell: UIInputViewAudioFeedback {
func enableInputClicksWhenVisible() -> Bool {
return true
}
}
However, no sound is made when the button is pressed.
Thanks for any help.