gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:23:31 25 4
gpt4 key购买 nike

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

最佳答案

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

就是说,如果您在后台线程上进行一些应用设置无论如何都需要一些时间,或者您只是希望用户在交互之前等待更长时间以便您可以显示 gif/视频,您可以做什么,您可以使静态图像与 gif/视频的第一帧匹配,并让您的入口点成为显示实际 gif/视频的 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)
}
}

关于ios - 如何使用 swift 在 Xcode 6 中将 GIF/视频添加到登陆屏幕背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261692/

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