我目前有一个应用程序,当用户点击按钮时,会在VC中添加一个视图,以显示视频(由用户拍摄)。按钮也会添加到视图中,当用户按下按钮时,应删除视图附带的所有视图。
如何删除视图?
目前我已经尝试过:
self.view.superview?.removeFromSuperview()
这不起作用。
以下是所有相关代码:
func newViewVideoPlayback(videoURL: URL!) {
let newView = UIImageView(frame: camPreview.bounds)
self.view.superview?.addSubview(newView)
//cancelButton create dec 1
let cancelButton = UIButton(frame: CGRect(x: 0, y: 589, width: 145, height: 78))
cancelButton.setTitle("Cancel", for: .normal)
cancelButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
cancelButton.setTitleColor(.red, for: .normal)
cancelButton.layer.cornerRadius = 8
cancelButton.backgroundColor = .white
self.view.superview?.addSubview(cancelButton) //maybe do newView, then subview
playerItem1 = AVPlayerItem(url: videoURL as URL)
playerQueue = AVQueuePlayer(playerItem: playerItem1)
playerLayer = AVPlayerLayer(player: playerQueue)
playerLooper = AVPlayerLooper(player: playerQueue, templateItem: playerItem1)
playerLayer.frame = newView.bounds //shud it be view.bounds
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
newView.layoutIfNeeded() //same as other view comment
newView.layer.insertSublayer(playerLayer, at: 0)
//newView.superview?.bringSubview(toFront: newView)
self.playerQueue?.play()
}
按钮:
@objc func buttonAction(sender: UIButton!) {
print("Button tapped")
//for cancelButon
//playerQueue = nil
self.view.superview?.removeFromSuperview()
}
在添加视频网址的方法中调用了func
答案 0 :(得分:1)
您可能想做的是从ViewController中删除newView
,而不是view
的{{1}}。
为superview
创建一个全局变量:
newView
...然后像这样分配class YourViewController: UIViewController {
var newView: UIImageView?
...
:
newView
...,当您需要删除此视图时,只需调用以下内容:
func newViewVideoPlayback(videoURL: URL!) {
newView = UIImageView(frame: camPreview.bounds)
...
您的按钮操作应如下所示:
newView?.removeFromSuperview()