簡體   English   中英

如何在不將手指從屏幕抬起的情況下結束長按手勢?

[英]How to end long press gesture without lifting finger from screen?

我試圖結束長按手勢,而又不將手指從屏幕上抬起。 那有可能很快嗎?

我正在制作一個可讓您錄制視頻的應用程序。 當我按下按鈕時,視頻記錄開始;當我從屏幕上抬起手指時,視頻記錄結束。 該部分工作完美。 我還想讓長按手勢在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()
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM