gpt4 book ai didi

ios - 如何在不离开屏幕的情况下结束长按手势?

转载 作者:行者123 更新时间:2023-11-30 12:17:09 27 4
gpt4 key购买 nike

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

我正在制作一个可让您录制视频的应用程序。当我按下按钮时视频录制开始,当我从屏幕上抬起手指时视频录制结束。那部分工作完美。我还希望如果我的手指仍然按在按钮上,长按手势会在 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()
}
}

最佳答案

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

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

}

关于ios - 如何在不离开屏幕的情况下结束长按手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com