gpt4 book ai didi

swift - 如何在 YTPlayerView 中隐藏控件?

转载 作者:可可西里 更新时间:2023-11-01 00:37:15 83 4
gpt4 key购买 nike

我正在使用 youtube-ios-player-helper 库在我的应用程序中播放 youtube 视频。在您按下播放按钮和视频开始全屏播放之间有一小段时间延迟。所以我想在这段时间里隐藏控件。

我该怎么做?

这是我现在在 Swift 中拥有的:

protocol YouTubePlayerViewDelegate {
func playVideo(id: String)
}

class DeferredYouTubePlayerView : UIView, EventActionable, YTPlayerViewDelegate {
var youtubeId: String!
var origin: String!
var playerWidth: Int!
var imageView: ImageLoadingBackgroundView?
var playImageView: UIImageView?
var player: YTPlayerView?
var backDropView: UIView?

var eventCategory:String!
var touchCancelled = false

let playerVars:[NSObject:AnyObject] = [
"playsinline":0,
"autoplay" : 0,
"modestbranding" : 1,
"origin":MetroManager.sharedInstance.selectedMetro!.baseURL
]

var delegate : YouTubePlayerViewDelegate?

func loadVideo() {
backgroundColor = UIColor.blackColor()

if imageView == nil {
imageView = ImageLoadingBackgroundView()
imageView!.setTranslatesAutoresizingMaskIntoConstraints(false)
imageView?.constrain(.FillParent(self))
}

if playImageView == nil {
playImageView = UIImageView(image: UIImage(named: "play_button"))
playImageView?.constrain(.Height(42))
playImageView?.constrain(.Height(42))
playImageView?.constrain(.CenterInParent(imageView!))
}

if let url = buildImageURL() {
imageView?.alpha = 1
imageView!.imageURL = url
} else {
imageView?.alpha = 0
}

if backDropView == nil {
backDropView = UIView()
backDropView?.backgroundColor = UIColor.blackColor()
backDropView?.constrain(.CenterInParent(self))
backDropView?.constrain(.FillParent(self))
addSubview(backDropView!)
}

if player == nil {
player = YTPlayerView()
player?.constrain(.CenterInParent(self))
player?.constrain(.FillParent(self))
player?.delegate = self
player!.loadWithVideoId(youtubeId, playerVars: playerVars)
addSubview(player!)

bringSubviewToFront(imageView!)
}
}

func buildImageURL() -> NSURL? {
return NSURL(string: "http://img.youtube.com/vi/\(youtubeId)/0.jpg")
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let playImageView = playImageView {
playImageView.image = UIImage(named: "play_button_highlighted")
}
}

//If the user moves their finger, they probably want to slide to the next view, cancel touches
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let playImageView = playImageView {
playImageView.image = UIImage(named: "play_button")
}

touchCancelled = true
}

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) {
switch state {
case .Buffering:
fallthrough
case .Playing:
hidePoster()
case .Ended:
fallthrough
case .Paused:
showPoster()
default:
()
}
}

func hidePoster() {
UIView.animateWithDuration(DEFAULT_ANIMATION_TIME, animations: { () -> Void in
self.imageView?.alpha = 0
})
}

func showPoster() {
UIView.animateWithDuration(DEFAULT_ANIMATION_TIME, animations: { () -> Void in
self.imageView?.alpha = 1
})
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
println("ended")

if let playImageView = playImageView {
playImageView.image = UIImage(named: "play_button")
}

if(!touchCancelled){
loggerA(category: eventCategory, action: "Cover Scroll", label: "Video Play")
// delegate?.playVideo(youtubeId)
hidePoster()
player!.playVideo()

}

touchCancelled = false
}

deinit{
//delegate = nil
}
}

最佳答案

哦,我刚刚找到了解决方案:我需要在 playerVars 中将“控件”设置为 (0) false 值;所以在我的上下文中它看起来像这样: let playerVars:[NSObject:AnyObject] = [ "controls":0, ... ]

而且它真的奏效了!

关于swift - 如何在 YTPlayerView 中隐藏控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769683/

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