我想使用相同的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值就可以使用单个视图进行操作。 –