How to make a Button play while pressed and stop when released?

The previous post had errors I didn't notice. Sorry.


I have this to play a piece of music


@IBAction func play(_ sender: AnyObject) {

audioPlayer.play()


What I want to do is have the music play while the button is pressed and stop when it is released.

I have connected both touchDown and touchUpInside to the IBAction.

I have tried using many combinations of


if // touchDown {

audioPlayer.play()

} else if // touchUpInside {

audioPlayer.stop()

} else {

}


but I cant find a way to have the method recognise the touchDown and touchUpInside actions.

Can this be done?

Replies

I found a way to make this work but now get a loud clicking noise when the button is released.