我在Swift制作了一个应用程序,可以在点击按钮时录制语音。
我想知道需要执行哪些操作来记录,如下例所示:
是否有按钮@IBAction
来执行此操作?
答案 0 :(得分:4)
如果您打算应用以下方案:
您可以通过在录制按钮中添加以下事件来实现它:
1-添加touchDown
event =>用户触摸了按钮(并没有释放),因此开始录制。
2-添加touchUpInside
event =>一旦用户发布录制结束。
3-添加touchDragExit
event =>或者拖出按钮。
它应该类似于:
@IBAction func touchDown(sender: AnyObject) {
print("Start recording")
}
@IBAction func touchUpInside(sender: AnyObject) {
print("Stop recording")
}
@IBAction func touchDragExit(sender: AnyObject) {
print("Stop recording")
}
答案 1 :(得分:1)
您可以使用LongPressGestureRecognizer方法开始和停止录制语音
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("handleLongTapOnButton:")))
btnRecordVoice.addGestureRecognizer(longGestureRecognizer)
}
func handleLongTapOnButton(sender : UIGestureRecognizer){
print("Long tap is handled")
if sender.state == .began {
print("UILongPressGestureRecognizerStateBegan so start the recording voice here")
//write the function for start recording the voice here
}
else if sender.state == .ended {
print("UILongPressGestureRecognizerStateEnded so stop the recording voice here")
//write the function for stop recording the voice here
}
}