我想使用相同的UIView類來錄製和播放聲音。在這個視圖類中,我通過編程添加了一個按鈕,並且我想根據視圖的目的更改按鈕文本。當視圖作爲播放器創建時,它將顯示播放圖標(或現在的文本)。如果視圖是作爲錄像機創建的,則會顯示錄像圖標。但是我無法從UIButton的視圖中訪問全局變量。從編程的uibutton屬性訪問全局變量
class AudioPlayerView: UIView {
var isRecorder = false
override init(frame: CGRect) {
super.init(frame: frame)
...
}
let theButton: UIButton = {
let button = UIButton()
if isRecorder {
button.setTitle("▷", for: .normal)
button.addTarget(self, action: #selector(playPauseAudio), for: .touchUpInside)
} else {
button.setTitle("▢", for: .normal)
button.addTarget(self, action: #selector(recordTapped), for: .touchUpInside)
}
button.backgroundColor = .clear
button.layer.cornerRadius = 15.0
return button
}()
你的意思是說你無法訪問isRecorder變量? –
是Syed。究竟。但是,當我將「isRecorder」放在課程減速上方時,它會工作... –
爲什麼要創建兩個視圖一個用於錄製,另一個用於播放,因爲您只需更改isRecorder值就可以使用單個視圖進行操作。 –