0

我试图在不从屏幕上抬起手指的情况下结束长按手势。这可能很快吗?

我正在制作一个可以让您录制视频的应用程序。视频录制在我按下按钮时开始,当我将手指从屏幕上移开时结束。那部分工作得很好。如果我的手指仍然按下按钮,我还想要长按手势在 30 秒后结束。我实际上让它停止录制,但问题是当录制停止时长按手势实际上并没有结束。

这是我的一些代码:

func stop() {
    let seconds : Int64 = 5
    let preferredTimeScale : Int32 = 1
    let maxDuration : CMTime = CMTimeMake(seconds, preferredTimeScale)
    movieOutput.maxRecordedDuration = maxDuration

    if movieOutput.recordedDuration == movieOutput.maxRecordedDuration {
       movieOutput.stopRecording()
    }
}

func longTap(_ sender: UILongPressGestureRecognizer){
    print("Long tap")

    stop()

    if sender.state == .ended {
        print("end end")
        movieOutput.stopRecording()
    }
    else if sender.state == .began {
        print("begin")
        captureSession.startRunning()
        startRecording()
    }
}
4

1 回答 1

1

您可以尝试使用计时器来取消手势:

class myController:UIViewController {
var timer:Timer! = nil
var lpr:UILongPressGestureRecognizer! = nil

override func viewDidLoad() {
    super.viewDidLoad()

    lpr = UILongPressGestureRecognizer()
    lpr.minimumPressDuration = 0.5
    lpr.numberOfTouchesRequired = 1
    // any other gesture setup
    lpr.addTarget(self, action: #selector(doTouchActions))
    self.view.addGestureRecognizer(lpr)


}

func createTimer() {
    if timer == nil {
    timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(cancelTrouchFromTimer), userInfo: nil, repeats: false)
    }
}

func cancelTimer() {
    if timer != nil {
    timer.invalidate()
    timer = nil
    }
}

func cancelTrouchFromTimer() {
    lpr.isEnabled = false
    //do all the things
    lpr.isEnabled = true

}

func doTouchActions(_ sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        createTimer()
    }

    if sender.state == .ended {// same for other states .failed .cancelled {
    cancelTimer()
    }

}

// cancel timer for all cases where the view could go away, like in deInit
 func deinit {
    cancelTimer()
}

}

于 2017-07-24T01:42:27.803 回答