logging声音的button动作
我在Swift上制作了一个应用程序,当你点击一个button时,它可以录制声音。
我想知道需要执行哪个操作来logging下面的例子:
- 当我点击button,录音机开始和
- 录音机录制声音直到手指放开button
有没有button@IBAction
做到这一点?
如果我没有错,你想要应用这个场景:
- 用户触摸了button(并没有释放),所以开始录制。
- 一旦用户释放 – 或拖出button,logging应该结束。
您可以通过将以下事件添加到录制button来实现此目的:
1-添加touchDown
事件=>用户触摸button(并没有释放),所以logging开始。
2-添加touchUpInside
事件=>一旦用户释放logging应该结束。
3-添加touchDragExit
事件=>或者拖出button。
它应该类似于这个:
@IBAction func touchDown(sender: AnyObject) { print("Start recording") } @IBAction func touchUpInside(sender: AnyObject) { print("Stop recording") } @IBAction func touchDragExit(sender: AnyObject) { print("Stop recording") }
希望这有助于。
您可以使用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 } }