取消 UILongPressGesture [英] Canceling a UILongPressGesture

查看:37
本文介绍了取消 UILongPressGesture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法取消 UILongPressGesture.手势连接到一个按钮,当按下按钮时,avaudiorecorder 开始录制音频.当我离开按钮时,我希望录音停止.感谢任何帮助,谢谢!

I'm having trouble canceling a UILongPressGesture. The gesture is connected to a button, and when the button is pressed, an avaudiorecorder starts recording audio. I want the audio recording to stop when I move off of the button. Any help is appreciated, thanks!

@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()
    }
}

编辑 - 取 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
    }   
}

编辑 - 取 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 在手势改变并且我从屏幕上松开手指后仍然被调用.

This code works, the only issue is that .Ended is still being called after the gesture has changed and I release my finger from the screen.

推荐答案

当长按手势识别器的触摸移动时调用该函数,状态为 UIGestureRecognizerStateChanged.

When the touch of a long press gesture recognizer moves then the function is called with the state UIGestureRecognizerStateChanged.

我会像这样更改您的代码...

I would change your code like this...

@IBAction func recordAudio(sender: AnyObject)
{
    switch (sender.state) {
        case .Ended:
            session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
            recorder.stop()
            recordButton.backgroundColor = .whiteColor()
            save()
        case .Began:
            session.setCategory(AVAudioSessionCategoryRecord, error: nil)
            recorder.record()
            recordButton.backgroundColor = .greenColor()
        case .Cancelled:
            recorder.stop()
            recordButton.backgroundColor = .redColor()
        case .Changed:
            // detect if the touch has gone outside of the button and stop the recording
        default:
            // do nothing
    }
}

用于检测触摸是否在按钮内部

你可以这样做..

let touchLocation = recognizer.location(in: recordButton)

if !recordButton.bounds.contains(touchLocation) {
// touch is outside of button
}

未测试或任何东西.刚刚在 Safari 中徒手输入.

Not tested or anything. Just typed freehand in Safari.

这篇关于取消 UILongPressGesture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆