gpt4 book ai didi

swift - 如何从 superView 中删除自定义的playerView?

转载 作者:行者123 更新时间:2023-11-30 12:17:12 27 4
gpt4 key购买 nike

我有一个videPlayerView,它上面有ContainerView来显示activityIndi​​catorView。当我从单元格内的 collectionView 中选择一个项目时, View 就会被加载。我的问题是如何使用cancelButton删除此 View ,我尝试了容器和playerView的removeFromSuperview(),但应用程序因此错误而崩溃

AQDefaultDevice (1): skipping input stream 0 0 0x0

代码如下:

class VideoPlayerView: UIView {
var videoUrl: String!
var uuidd: String!


let activityIndicatorView: UIActivityIndicatorView = {
let aiv = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
aiv.translatesAutoresizingMaskIntoConstraints = false
aiv.startAnimating()

return aiv
}()



lazy var controlsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0, alpha: 1)
view.isUserInteractionEnabled = true


view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlezoomout)))

return view
}()

func handlezoomout(hh: UITapGestureRecognizer){
print("n3me")


}


lazy var cancelButton: UIButton = {
let cancelButton = UIButton()
cancelButton.setImage(#imageLiteral(resourceName: "cancel"), for: UIControlState())
cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
return cancelButton
}()

func cancel() {

controlsContainerView.removeFromSuperview()
let video = VideoPlayerView()
video.removeFromSuperview()


}







lazy var pausePlayButton: UIButton = {
let button = UIButton(type: .system)
let image = UIImage(named: "pause")
button.setImage(image, for: UIControlState())
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = .white
button.isHidden = true

button.addTarget(self, action: #selector(handlePause), for: .touchUpInside)

return button
}()


var isPlaying = false

func handlePause() {
if isPlaying {
player?.pause()
pausePlayButton.setImage(UIImage(named: "play"), for: UIControlState())
} else {
player?.play()
pausePlayButton.setImage(UIImage(named: "pause"), for: UIControlState())
}

isPlaying = !isPlaying
}


override init(frame: CGRect) {
super.init(frame: frame)



setUpPlayerView()

controlsContainerView.frame = frame
addSubview(controlsContainerView)



cancelButton.frame = CGRect(x: 16.0, y: 20.0, width: 30.0, height: 30.0)
controlsContainerView.addSubview(cancelButton)


controlsContainerView.addSubview(activityIndicatorView)
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

controlsContainerView.addSubview(pausePlayButton)
pausePlayButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
pausePlayButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
pausePlayButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
pausePlayButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
backgroundColor = UIColor.black



}




var player: AVPlayer?

fileprivate func setUpPlayerView() {

let postQuery = PFQuery(className: "posts")
postQuery.whereKey("uuid", equalTo: PostUuidGlobalVariable.postuuid.last!)
postQuery.getFirstObjectInBackground { (object, error) in
if (error == nil && object != nil) {
let videoFile = object!["video"] as! PFFile

if let url = URL (string: videoFile.url!) {
self.player = AVPlayer(url: url)

let playerLayer = AVPlayerLayer(player: self.player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame





self.player?.play()
self.player?.isMuted = false

self.player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(note:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)


}
}
}
}




func playerDidFinishPlaying(note: NSNotification) {




}



override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

//this is when the player is ready and rendering frames
if keyPath == "currentItem.loadedTimeRanges" {
activityIndicatorView.stopAnimating()


pausePlayButton.isHidden = false
isPlaying = true
}
}




required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


class VideoLauncher: NSObject {




func showVideoPlayer() {

if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.isUserInteractionEnabled = true
view.backgroundColor = UIColor.white
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)




let height = keyWindow.frame.height
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height)


let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)



view.addSubview(videoPlayerView)




UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

view.frame = keyWindow.frame

}, completion: { (completedAnimation) in

//maybe we'll do something here later...





UIApplication.shared.isStatusBarHidden = true
})

keyWindow.addSubview(view)




}



}


}

最佳答案

这有可能与您在主线程之外更改用户界面有关。

来自 UIView 文档
'线程注意事项
对应用程序用户界面的操作必须发生在主线程上。因此,您应该始终调用用户界面 View 类来自在应用程序主线程中运行的代码。唯一可能不是严格必要的情况是在创建 View 对象本身时,但所有其他操作都应在主线程上进行。'

此外,您的取消函数会创建一个新的视频播放器 View ,然后尝试将其从其父级中删除,这看起来不正确。

您的取消回调可能应如下所示

func cancel() {
DispatchQueue.main.async { [unowned self] in
// to remove controls
self.controlsContainerView.removeFromSuperview()

// to remove video player view
self.view.removeFromSuperview()
}
}

关于swift - 如何从 superView 中删除自定义的playerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com