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

时间:2022-10-30 15:08:34

what is the most efficient way to add a GIF/Video to the background of the landing screen ( home screen or first view controller) of my app in Xcode? i.e apps like spotify, uber, insagram etc. Being that my app is universal, how would i make it fit accordingly?

在Xcode中,将GIF/视频添加到我的应用程序的着陆屏幕(主屏幕或第一视图控制器)的背景中最有效的方法是什么?我。像spotify、uber、insagram等应用程序,如果我的应用程序是通用的,我该怎么做呢?

3 个解决方案

#1


1  

Do you mean the first screen that is displayed after your app is launched? If so: unfortunately you can't have dynamic content; you won't be able to use a gif/video.

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

That said, what you can do if you have some app-setup on background threads that will take some time anyway, or if you simply want the user to wait longer before interaction so that you can display the gif/video, you can make the static image match the first frame of the gif/video, and have your your entry point be a ViewController that displays the actual gif/video. Because this would delay the time to interaction, though, this would never be recommended.

说,你能做什么如果你有app-setup后台线程,需要一些时间,或者如果你只是想让用户等待时间交互,这样您就可以显示gif /视频,您可以使静态图像匹配gif /视频的第一帧,和你你的入口点是一个ViewController显示实际的gif /视频。但是,因为这会延迟交互的时间,所以不建议这样做。

As for making it fit: as of iOS 8 Apple recommends using LaunchScreen.xib. With it you can use Auto Layout to achieve universality.

至于使之适合:iOS 8的苹果推荐使用LaunchScreen.xib。有了它,你可以使用自动布局来实现通用性。

To add a video you can use MPMoviePlayerController, AVPlayer, or if you're using SPritekit you can use an SKVideoNode.

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

EDIT (in response to follow-up comments):

编辑(回应后续评论):

An NSURL is a reference to a local or remote file. This link will give you a decent overview. Just copy the movie in and follow that guide.

NSURL是对本地或远程文件的引用。这个链接会给你一个不错的概述。只要把电影拷贝进去,跟着导游走就行了。

In addition to the MPMoviePlayerController solution Saqib Omer suggested, here's an alternative method that uses a UIView with an AVPlayerLayer. It has a button on top of the video as an example, since that's what you're looking for.

除了MPMoviePlayerController解决方案Saqib Omer之外,这里还有一个替代方法,它使用带有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)
    }
}

#2


0  

For playing video add following code, declare class variable var moviePlayer:MPMoviePlayerController! . Than in your viewDidLoad()

播放视频添加以下代码,声明类变量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 will play video. But to fit it you need to add layout contraints. See this link to add constraints pragmatically.

这将会播放视频。但是为了适应它,你需要添加布局限制。请参见此链接以实际地添加约束。

#3


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

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

#1


1  

Do you mean the first screen that is displayed after your app is launched? If so: unfortunately you can't have dynamic content; you won't be able to use a gif/video.

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

That said, what you can do if you have some app-setup on background threads that will take some time anyway, or if you simply want the user to wait longer before interaction so that you can display the gif/video, you can make the static image match the first frame of the gif/video, and have your your entry point be a ViewController that displays the actual gif/video. Because this would delay the time to interaction, though, this would never be recommended.

说,你能做什么如果你有app-setup后台线程,需要一些时间,或者如果你只是想让用户等待时间交互,这样您就可以显示gif /视频,您可以使静态图像匹配gif /视频的第一帧,和你你的入口点是一个ViewController显示实际的gif /视频。但是,因为这会延迟交互的时间,所以不建议这样做。

As for making it fit: as of iOS 8 Apple recommends using LaunchScreen.xib. With it you can use Auto Layout to achieve universality.

至于使之适合:iOS 8的苹果推荐使用LaunchScreen.xib。有了它,你可以使用自动布局来实现通用性。

To add a video you can use MPMoviePlayerController, AVPlayer, or if you're using SPritekit you can use an SKVideoNode.

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

EDIT (in response to follow-up comments):

编辑(回应后续评论):

An NSURL is a reference to a local or remote file. This link will give you a decent overview. Just copy the movie in and follow that guide.

NSURL是对本地或远程文件的引用。这个链接会给你一个不错的概述。只要把电影拷贝进去,跟着导游走就行了。

In addition to the MPMoviePlayerController solution Saqib Omer suggested, here's an alternative method that uses a UIView with an AVPlayerLayer. It has a button on top of the video as an example, since that's what you're looking for.

除了MPMoviePlayerController解决方案Saqib Omer之外,这里还有一个替代方法,它使用带有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)
    }
}

#2


0  

For playing video add following code, declare class variable var moviePlayer:MPMoviePlayerController! . Than in your viewDidLoad()

播放视频添加以下代码,声明类变量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 will play video. But to fit it you need to add layout contraints. See this link to add constraints pragmatically.

这将会播放视频。但是为了适应它,你需要添加布局限制。请参见此链接以实际地添加约束。

#3


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

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