如何使用swift在Xcode 6中添加GIF /视频到登陆屏幕背景?

时间:2015-03-25 16:49:48

标签: ios swift video xcode6 gif

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

3 个答案:

答案 0 :(得分:1)

您的意思是推出应用后显示的第一个屏幕吗?如果是这样:遗憾的是,您无法拥有动态内容;你不能使用gif /视频。

那就是说,如果你在后台线程上有一些应用程序设置需要一些时间,或者如果你只是希望用户在交互之前等待更长时间,以便你可以显示gif / video,你可以做什么可以使静态图像与gif / video的第一帧匹配,并使您的入口点成为显示实际gif / video的ViewController。但是,这会延迟交互时间,因此永远不会建议这样做。

至于适合它:从iOS 8 Apple recommends using LaunchScreen.xib开始。有了它,您可以使用自动布局来实现普遍性。

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

EDIT(回应后续评论):

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

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

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)
    }
}

答案 1 :(得分:0)

要播放视频,请添加以下代码,声明类变量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链接以实用方式添加约束。

答案 2 :(得分:0)

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