gpt4 book ai didi

ios - Swift 中的模态视图 Controller

转载 作者:行者123 更新时间:2023-11-30 14:01:04 26 4
gpt4 key购买 nike

我有 VideoViewController.swift,我想在按下按钮时在第二个 View Controller 中播放视频,就像这张照片(不是全屏)

http://i.stack.imgur.com/xBB4d.png]

我尝试使用模态视图 Controller ,但它不起作用......问题是什么?

这是源代码:VideoViewController.swift

import UIKit
import AVKit
import AVFoundation

class VideoViewController: UIViewController {

@IBOutlet weak var AVPlayerView: UIView!

override func shouldAutorotate() -> Bool {
return false
}

override func viewDidLoad() {
super.viewDidLoad()

// yoon // from web
let sampleURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
let player = AVPlayer(URL: sampleURL)


let playerLayer = AVPlayerLayer(player: player)

let videoWidth = 320
let videoHeigh = 180
let screenWidth = self.view.frame.size.width
let screenHeigh = self.view.frame.size.height

playerLayer.frame = CGRectMake(
(screenWidth/2 - CGFloat(videoWidth/2)),
(screenHeigh/2 - CGFloat(videoHeigh/2)),
CGFloat(videoWidth),
CGFloat(videoHeigh))

AVPlayerView.layer.addSublayer(playerLayer)


player.play()

//AVPlayerView.backgroundColor = UIColor.blackColor()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

View Controller 。 swift

import UIKit
import Social
import AVKit
import AVFoundation

class ViewController: UIViewController, SideBarDelegate {

@IBOutlet weak var AVPlayerView: UIView!

override func shouldAutorotate() -> Bool {
return false
}

@IBAction func videoStream(sender: AnyObject) {


self.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
// Cover Vertical is necessary for CurrentContext
self.modalPresentationStyle = .CurrentContext
// Display on top of current UIView


self.presentViewController(VideoViewController(), animated: true, completion: nil)
//self.preferredContentSize = CGSizeMake(320, 200)
}

最佳答案

您的代码中有几个强制解包的选项。在打开包装之前,您需要确保 socket 已设置好。下面将在第二个 vc 中播放视频,但您需要添加帧和其他您想要的东西。第一个 vc 就很好

import UIKit
import AVKit
import AVFoundation

class VideoViewController: AVPlayerViewController {

@IBOutlet weak var AVPlayerView: UIView!

override func shouldAutorotate() -> Bool {
return false
}

private func playVideo() {
player = AVPlayer(URL: NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)
}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}

}

关于ios - Swift 中的模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989892/

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