我试图结束长按手势,而不是从屏幕上抬起手指。这有可能迅速吗?
我正在制作一款可让您录制视频的应用。当我按下按钮时视频录制开始,当我从屏幕上抬起手指时结束录像。那部分完美无缺。我还想要的是,如果我的手指仍然按下按钮,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()
}
}
答案 0 :(得分:0)
您可以尝试使用计时器来取消手势:
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()
}
}