我的應用程序中有一個按鈕,並且有些奇怪的時刻,有時當我點擊按鈕時,它並不總是執行所需的代碼。該應用程序不會崩潰,但如果我退出該應用程序並再次打開該按鈕將繼續不起作用。解決此問題的唯一方法是關閉手機或刪除並重新下載應用程序。代碼中的按鈕並不總是執行操作的問題
注意:這隻發生在一段時間,我不知道爲什麼。問題可能是一個約束問題嗎?這是我的按鈕的代碼。
@IBAction func recordVideoButtonPressed(_ sender: Any) {
if self.movieFileOutput.isRecording {
clipsCollectionView.reloadData()
self.movieFileOutput.stopRecording()
styleWhileNotRecording()
turnFlashOff()
stopTimer()
} else {
self.movieFileOutput.connection(withMediaType: AVMediaTypeVideo).videoOrientation = self.videoOrientation()
self.movieFileOutput.maxRecordedDuration = self.maxRecordedDuration()
self.movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath:self.videoFileLocation()), recordingDelegate: self)
styleWhileRecording()
turnOnFlash()
startTimer()
}
updateRecordButtonTitle()
}
這是我updateRecordButton方法。這是我如何知道 問題,因爲圖像並不總是更新。
func updateRecordButtonTitle() {
recordButton.alpha = 0
if !self.movieFileOutput.isRecording {
UIView.animate(withDuration: 0.3, animations: {
let stop = UIImage(named: "stopButton.png")
self.recordButton.setImage(stop, for: .normal)
self.recordButton.alpha = 1
})
} else {
UIView.animate(withDuration: 0.3, animations: {
let start = UIImage(named: "recordImage.png")
self.recordButton.setImage(start, for: .normal)
self.recordButton.alpha = 1
})
}
}
更新!
我剛剛注意到,當發生此問題時,計時器也不停止。所以我假設問題在於它沒有正確讀取。任何人有任何想法,爲什麼發生這種情況?
if self.movieFileOutput.isRecording {
UPDATE
我才意識到另一個問題。我在我的委託捕獲方法中添加了打印語句。
func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {
print(42332)
}
func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
cropVideo(outputFileURL)
print("Recording Finished")
}
發生此問題時...打印所有這些語句。它打印
42322和錄製完成。
我意識到當它發生時會記錄視頻並立即停止。我不知道爲什麼會發生這種情況?
不可能知道發生了什麼事情;請設置幾個斷點並告訴我們**哪些**分支會執行(您的if/else語句)。 –
在代碼似乎不起作用的地方設置斷點?好吧,我會嘗試這個,即使這種情況發生時應用程序不會崩潰,這仍然會有幫助嗎? –
您正試圖弄清楚發生了什麼,但信息太少。 '如果self.movieFileOutput.isRecording {...} else {...}'被執行了哪一個?是否調用updateRecordButtonTitle()?如果是這樣,那麼執行'if!self.movieFileOutput.isRecording {...} else {...}'中的哪一個?也許IBAction沒有被稱爲開始? –