0

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
    }
}

ただし、ボタンを押しても音は出ません。

助けてくれてありがとう。

4

2 に答える 2

1

キーボードの文字音を再生するには:-

enum SystemSound: UInt32 {

    case pressClick    = 1123
    case pressDelete   = 1155
    case pressModifier = 1156

    func play() {
        AudioServicesPlaySystemSound(self.rawValue)
    }

}

ここでも適切なサウンドの詳細を見つけてください。

したがって、次のように置き換えUIDevice.current.playInputClick()ますAudioServicesPlaySystemSound(systemSoundsID)

于 2018-05-07T11:23:11.857 に答える
0

受け入れられた回答と元の質問を使用して完全を期すために:

import AudioToolbox
import UIKit

enum SystemSound: UInt32 {

    case click = 1123
    case delete = 1155
    case modifier = 1156

    func play() {
        AudioServicesPlaySystemSound(self.rawValue)
    }

}

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() {
        isOn = !isOn
        let systemSound: SystemSound = (isOn) ? .click : .modifier
        systemSound.play()
    }

}
于 2018-05-07T11:54:39.383 に答える