2017-07-23 26 views
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() 
    } 
} 
+1

到底是怎麼了?你的問題不清楚。你有什麼嘗試?你有什麼問題? – rmaddy

+0

對不起。我製作的應用可以讓你錄製視頻。錄像開始時,我按下按鈕,結束時,我從屏幕上擡起我的手指。那部分工作完美。我還想要的是,如果我的手指仍然按下按鈕,則長按手勢將在30秒後結束。我實際上已經知道它停止錄製,但問題在於,當錄製停止時,長按手勢實際上並未結束。 –

+0

所有這些細節都屬於你的問題,而不是評論。 – rmaddy

回答

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

}

相關問題