2015-11-19 40 views
3

編輯:它不是標記鏈接的副本。如果仔細觀察,問題就不同了。當進入背景時視頻頂部的遮罩視圖

如何在視頻進入背景時在視頻頂部顯示遮罩視圖?我正在研究一個密碼保護的內容應用程序,它需要掩碼屏幕來隱藏內容,當它進入後臺(這樣雙按主屏幕按鈕不會顯示內容或視頻)。如果沒有視頻播放但在任何視頻(例如YouTube)上失敗,我的屏蔽視圖邏輯工作正常,視頻快照清晰可見。

Need to mask the video from background

對一般問題,但不是當視頻正在播放這post會談。對於視頻,問題依然存在。我到目前爲止的代碼。

//AppDelegate.swift 

var maskView: UIView! 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {   
    maskView.backgroundColor = UIColor.redColor() 
    return true 
} 
func applicationDidEnterBackground(application: UIApplication) { 
    window?.addSubview(maskView) 
} 

func applicationWillEnterForeground(application: UIApplication) { 
    maskView.removeFromSuperview() 
} 
+0

[控制在了iOS 7多任務切換的屏幕截圖]的可能的複製(http://stackoverflow.com/questions/18959411/controlling-the-screenshot-in-the-ios-7-多任務切換器) – JAL

+0

@JAL它不是重複的。我的問題是針對播放時視頻背景。 – xoail

回答

2

你的問題是你使用錯誤的方法。當您按兩次主頁按鈕時不會調用applicationDidEnterBackground。而不是這種方法,我建議你使用applicationWillResignActive即使用戶打開NotificationCenter也會被調用。我用下面的代碼測試你的案例。

// appDelegate: 
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    maskView = UIView(frame: UIScreen.mainScreen().bounds) 
    maskView.backgroundColor = UIColor.redColor() 
    return true 
} 

func applicationWillResignActive(application: UIApplication) { 
    window?.addSubview(maskView) 
} 

func applicationWillEnterForeground(application: UIApplication) { 
    maskView.removeFromSuperview() 
} 

// PlayerViewController 
@IBOutlet weak var videoView: PlayerView! // root view of current controller 
let player = AVPlayer(URL: NSURL(string: "http://www.ebookfrenzy.com/ios_book/movie/movie.mov")!) 

override func viewDidLoad() { 
    videoView.setPlayer(player) 
} 

override func viewDidAppear(animated: Bool) { 
    player.play() 
} 

// PlayerView 
override class func layerClass() -> AnyClass { 
    return AVPlayerLayer.classForCoder() 
} 

func setPlayer(player: AVPlayer) { 
    (layer as! AVPlayerLayer).player = player 
} 

UPD:我的猜測是不是成功的,因爲你不給我更多的信息。但現在我明白了:問題是,當您播放嵌入式視頻時,會創建與您的視頻重疊的新window。在這個window裏面,你有一堆不同視圖的玩家(你可以用pviews命令chisel工具看到這個信息)。爲了解決這種情況,我使用下面的代碼。 (AppDelegate中)

var maskViews: [UIView] = [] 

func maskView() -> UIView { 
    let view = UIView(frame: UIScreen.mainScreen().bounds) 
    view.backgroundColor = UIColor.redColor() 
    return view 
} 

func applicationWillResignActive(application: UIApplication) { 
    for window in UIApplication.sharedApplication().windows { 
     let mask = maskView() 
     maskViews.append(mask) 
     window.addSubview(mask) 
    } 
} 

func applicationDidBecomeActive(application: UIApplication) { 
    for view in maskViews { 
     view.removeFromSuperview() 
    } 
    maskViews.removeAll() 
} 
+0

applicationWillResignActive對我有同樣的效果。我必須補充說,我正嘗試從嵌入式WKWebView播放YouTube視頻。 – xoail

+0

@xoail嘗試更新解決方案 – rkyr

+0

謝謝你一個非常可靠的解決方案! – xoail