如何使用 swift 在 Xcode 6 中将 GIF/Video 添加到登录屏幕背景?

How do I Add A GIF/Video To Landing Screen Background In Xcode 6 using swift?

在 Xcode 中将 GIF/Video 添加到我的应用程序的登录屏幕(主屏幕或第一个视图控制器)背景的最有效方法是什么?即 spotify、uber、insagram 等应用程序。由于我的应用程序是通用的,我将如何使它相应地适合?

您是指您的应用启动后显示的第一个屏幕吗?如果是这样:不幸的是你不能有动态内容;您将无法使用 gif/video。

就是说,如果您在后台线程上进行一些应用程序设置无论如何都需要一些时间,或者您只是希望用户在交互之前等待更长时间以便您可以显示 gif/video,您可以使静态图像匹配 gif/video 的第一帧,并将您的入口点设置为显示实际 gif/video 的 ViewController。但是,因为这会延迟交互时间,所以永远不会推荐。

至于合身:截至 iOS 8 Apple recommends using LaunchScreen.xib。有了它你就可以使用Auto Layout来实现通用性了

要添加视频,您可以使用 MPMoviePlayerController、AVPlayer,或者如果您使用的是 SPritekit,则可以使用 SKVideoNode。

编辑(回应后续评论):

NSURL 是对本地或远程文件的引用。 This link 会给你一个体面的概述。只需复制电影并按照该指南操作即可。

除了 Saqib Omer 建议的 MPMoviePlayerController 解决方案之外,这里还有一个使用带有 AVPlayerLayer 的 UIView 的替代方法。作为示例,它在视频顶部有一个按钮,因为这就是您要查找的内容。

import AVKit
import AVFoundation
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Start with a generic UIView and add it to the ViewController view
        let myPlayerView = UIView(frame: self.view.bounds)
        myPlayerView.backgroundColor = UIColor.blackColor()
        view.addSubview(myPlayerView)

        // Use a local or remote URL
        let url = NSURL(string: "http://eoimages.gsfc.nasa.gov/images/imagerecords/76000/76740/iss030-e-6082_sdtv.mov") // See the note on NSURL above.

        // Make a player
        let myPlayer = AVPlayer(URL: url)
        myPlayer.play()

        // Make the AVPlayerLayer and add it to myPlayerView's layer
        let avLayer = AVPlayerLayer(player: myPlayer)
        avLayer.frame = myPlayerView.bounds
        myPlayerView.layer.addSublayer(avLayer)

        // Make a button and add it to myPlayerView (you'd need to add an action, of course)
        let myButtonOrigin = CGPoint(x: myPlayerView.bounds.size.width / 3, y: myPlayerView.bounds.size.height / 2)
        let myButtonSize = CGSize(width: myPlayerView.bounds.size.width / 3, height: myPlayerView.bounds.size.height / 10)
        let myButton = UIButton(frame: CGRect(origin: myButtonOrigin, size: myButtonSize))
        myButton.setTitle("Press Me!", forState: .Normal)
        myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        myPlayerView.addSubview(myButton)
    }
}

为了播放视频添加以下代码,声明 class 变量 var moviePlayer:MPMoviePlayerController! 。比你的 viewDidLoad()

var url:NSURL = NSURL(string: "YOUR_URL_FOR_VIDEO")



moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 0, y: 0, width: 200, height: 150)



 self.view.addSubview(moviePlayer.view)
  moviePlayer.fullscreen = true



moviePlayer.controlStyle = MPMovieControlStyle.Embedded

这将播放视频。但要适应它,您需要添加布局约束。请参阅 this link 以务实地添加约束。

import MediaPlayer

class ViewController: UIViewController {

    var moviePlayer: MPMoviePlayerController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load the video from the app bundle.
        let videoURL: NSURL = NSBundle.mainBundle().URLForResource("video", withExtension: "mp4")!

        // Create and configure the movie player.
        self.moviePlayer = MPMoviePlayerController(contentURL: videoURL)

        self.moviePlayer.controlStyle = MPMovieControlStyle.None
        self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill

        self.moviePlayer.view.frame = self.view.frame
        self.view .insertSubview(self.moviePlayer.view, atIndex: 0)

        self.moviePlayer.play()

        // Loop video.
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loopVideo", name: MPMoviePlayerPlaybackDidFinishNotification, object: self.moviePlayer)
    }

    func loopVideo() {
        self.moviePlayer.play()
    }

https://medium.com/@kschaller/ios-video-backgrounds-6eead788f190#.2fhxmc2da