我正在構建一些類似於SnapChat的功能。按住一個視圖,它會調出一個電影,然後在電影完成時(當前正在工作),或者當用戶拿起他們的手指(不工作,這就是這個問題的意思)返回到主視圖控制器, 。斯威夫特:長按取消電影播放。我如何堅持長按?
我的問題在於IBAction,當視頻出現時,UIGestureRecognizerState
從Began
變爲Cancelled
。
我想爲國家不改變,直到用戶提起他們的手指,這應該註冊UIGestureRecognizerState
爲Ended
如何做到這將是真棒任何提示。謝謝!
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerViewController!
@IBAction func handleLongPress(recognizer: UILongPressGestureRecognizer) {
println("\(recognizer)")
if recognizer.state == UIGestureRecognizerState.Began {
playVideo()
}
if recognizer.state == UIGestureRecognizerState.Ended {
self.moviePlayer.moviePlayer.stop()
}
}
func videoHasFinishedPlaying(notification: NSNotification){
println("Video finished playing")
}
func playVideo() {
// get path and url of movie
let path = NSBundle.mainBundle().pathForResource("IMG_8602", ofType:"MOV")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(moviePlayer)
// construct the views
moviePlayer.view.frame = self.view.bounds
self.view.addSubview(moviePlayer.view)
// remove controls at top and bottom of video
moviePlayer.moviePlayer.controlStyle = MPMovieControlStyle.None
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoHasFinishedPlaying:",
name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
}
}
編輯:一個可能的解決方案
因此,這是一個總的僥倖,但我想通了如何在這種情況下做到這一點。
我只是從我的playVideo
函數中刪除了行presentMoviePlayerViewControllerAnimated(moviePlayer)
。
不完全確定這是爲什麼工作,但它適用於我的上下文。
我有一個'類似'的問題。不一樣的事情,但它涉及從長手勢到拖動手勢http://stackoverflow.com/questions/27790899/begin-uipangesture-event-from-a-pressed-state-at-time-of-instantiation/ 27791190#27791190 – Aggressor 2015-01-20 23:22:17
它不會回答你的問題,但我會說我找不到方法來通過這個手勢。我認爲你最好的選擇是用手勢監聽器添加超級視圖,並在其下添加子視圖。然後,您可以從超級視圖中微調管理事件,這些超級視圖可以處理單點觸摸(當另一個視圖添加到頂端時不會取消,因爲現在將其添加到下方) – Aggressor 2015-01-20 23:23:21
感謝鏈接@Aggressor。任何方式,你可以告訴我如何以你的方式做到這一點?隨意使用我的代碼,或者只是粘貼一個要點,如果你自己的代碼,我可以弄明白。 – 2015-01-20 23:24:50