У меня возникли проблемы с отменой UILongPressGesture. Жест подключен к кнопке, и когда кнопка нажата, avaudiorecorder начинает запись звука. Я хочу, чтобы запись звука прекращалась, когда я отключаюсь от кнопки. Любая помощь приветствуется, спасибо!Отмена UILongPressGesture
@IBAction func recordAudio(sender: AnyObject)
{
if sender.state == UIGestureRecognizerState.Ended
{
session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
recorder.stop()
recordButton.backgroundColor = UIColor.whiteColor()
save()
}
else if sender.state == UIGestureRecognizerState.Began
{
session.setCategory(AVAudioSessionCategoryRecord, error: nil)
recorder.record()
recordButton.backgroundColor = UIColor.greenColor()
}
else if sender.state == UIGestureRecognizerState.Cancelled
{
recorder.stop()
recordButton.backgroundColor = UIColor.redColor()
}
}
EDIT - Take 2
@IBAction func recordAudio(sender: UILongPressGestureRecognizer)
{
switch (sender.state)
{
case .Ended:
session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
recorder.stop()
recordButton.backgroundColor = UIColor.whiteColor()
save()
case .Began:
session.setCategory(AVAudioSessionCategoryRecord, error: nil)
recorder.record()
recordButton.backgroundColor = UIColor.greenColor()
case .Cancelled, .Ended:
recorder.stop()
recordButton.backgroundColor = UIColor.redColor()
default:
break
}
}
EDIT - Возьмите 3 (почти нет)
@IBAction func recordAudio(sender: UILongPressGestureRecognizer)
{
switch (sender.state)
{
case .Ended:
session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
recorder.stop()
recordButton.backgroundColor = UIColor.whiteColor()
post()
case .Began:
session.setCategory(AVAudioSessionCategoryRecord, error: nil)
recorder.record()
recordButton.backgroundColor = UIColor.greenColor()
case .Cancelled:
recorder.stop()
recordButton.backgroundColor = UIColor.redColor()
case .Changed:
let touchLocation = recordGesture.locationInView(recordButton)
if (!CGRectContainsPoint(recordButton.bounds, touchLocation))
{
// touch is outside of button
recorder.stop()
recordButton.backgroundColor = UIColor.whiteColor()
break
}
default:
break
}
}
Этот код работает, единственная проблема в том, что .Ended еще вызываемый после того, как жест изменился, и я отбрасываю палец с экрана.
Вы можете попробовать добавить упражнение к кнопке 'UIControlEventTouchDragOutside'. Вы уверены, что не сможете получить эту функциональность от UIButton, я не уверен, что вам нужен жест. – BooRanger
Кнопка не получит действие, потому что устройство распознавания касания уже коснулось касания. – Fogmeister
так ли решение удаляет longpressgesture вообще? – user3353890