【问题标题】:Loading MPMoviePlayerViewController with transparent background in Swift?在 Swift 中加载具有透明背景的 MPMoviePlayerViewController?
【发布时间】:2015-03-23 05:12:26
【问题描述】:

我在 Swift (iOS 8.1) 中使用 MPMoviePlayerViewController,并根据How to play a local video with Swift? 的答案使用代码,这一切都很好。

这段代码如下:

    let path = NSBundle.mainBundle().pathForResource("movie",     ofType:"m4v")
    let url = NSURL.fileURLWithPath(path!)
    moviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = moviePlayer {
        player.view.frame = self.animationImage.frame
        player.backgroundView.backgroundColor = UIColor.clearColor()
        player.view.backgroundColor = UIColor.clearColor()

        for subView in player.view.subviews  {
            subView.backgroundColor = UIColor.clearColor()
        }

        player.prepareToPlay()
        player.scalingMode = .AspectFill
        player.controlStyle = .None
        self.view.addSubview(player.view)
    }

但我试图让电影播放器​​的背景透明。我找到了使用 Objective-C 的答案,但我在编译时遇到了错误。我目前拥有的代码是:

player.backgroundView.backgroundColor = UIColor.clearColor()
player.view.backgroundColor = UIColor.clearColor()

for subView in player.view.subviews  {
     subView.backgroundColor = UIColor.clearColor()
} 

编译错误发生在 subView.backgroundColor = UIColor.clearColor() 行。错误是:

'@lvalue $T4' 不等于'CGColor!!'

对于我在这里做错的任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift uiview mpmovieplayercontroller


    【解决方案1】:

    我认为您可能希望指定 subView 的类型为 UIView

    for subView in moviePlayer!.view.subviews as [UIView] {
        subView.backgroundColor = UIColor.clearColor()
    }
    

    【讨论】:

    • 太棒了!谢谢胡言乱语。你回答了我的问题,但不幸的是我仍然无法让动画的背景变得透明。谢谢。
    • 使用 clearColor 就足够了,如果你的self.view 有红色这样的背景颜色,使用scalingMode = .AspectFit,你会在播放电影时看到红色。您可以随时设置player.view 的边框颜色和边框宽度,以查看您的电影是否填充在其帧中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多