简体   繁体   English

如何在iOS中全屏显示Twilio远程视频视图“ TVIVideoView”

[英]How to make Twilio remote Video view “TVIVideoView” full screen in iOS

I am using Twilio for Video calling and its working fine but the only issue is unable to set the full screen for remote video.Below code is a way to create remote video setup by Twilio in their QuickStart Project . 我正在使用Twilio进行视频通话,并且可以正常工作,但是唯一的问题是无法为远程视频设置全屏。下面的代码是Twilio在其QuickStart Project中创建远程视频设置的一种方法。

Xcode Version: 9.3 Swift Version: 4.1 Xcode版本:9.3 Swift版本:4.1

func setupRemoteVideoView() {
    // Creating `TVIVideoView` programmatically
    self.remoteView = TVIVideoView.init(frame: CGRect.zero, delegate:self)
    self.view.insertSubview(self.remoteView!, at: 0)

    // `TVIVideoView` supports scaleToFill, scaleAspectFill and scaleAspectFit
    // scaleAspectFit is the default mode when you create `TVIVideoView` programmatically.
    self.remoteView!.contentMode = .scaleAspectFit;

    let centerX = NSLayoutConstraint(item: self.remoteView!,
                                     attribute: NSLayoutAttribute.centerX,
                                     relatedBy: NSLayoutRelation.equal,
                                     toItem: self.view,
                                     attribute: NSLayoutAttribute.centerX,
                                     multiplier: 1,
                                     constant: 0);
    self.view.addConstraint(centerX)
    let centerY = NSLayoutConstraint(item: self.remoteView!,
                                     attribute: NSLayoutAttribute.centerY,
                                     relatedBy: NSLayoutRelation.equal,
                                     toItem: self.view,
                                     attribute: NSLayoutAttribute.centerY,
                                     multiplier: 1,
                                     constant: 0);
    self.view.addConstraint(centerY)
    let width = NSLayoutConstraint(item: self.remoteView!,
                                   attribute: NSLayoutAttribute.width,
                                   relatedBy: NSLayoutRelation.equal,
                                   toItem: self.view,
                                   attribute: NSLayoutAttribute.width,
                                   multiplier: 1,
                                   constant: 0);
    self.view.addConstraint(width)
    let height = NSLayoutConstraint(item: self.remoteView!,
                                    attribute: NSLayoutAttribute.height,
                                    relatedBy: NSLayoutRelation.equal,
                                    toItem: self.view,
                                    attribute: NSLayoutAttribute.height,
                                    multiplier: 1,
                                    constant: 0);
    self.view.addConstraint(height)
}

Here's your code tidied a little: 这是整理一下您的代码:

  func setupRemoteVideoView() {
        // Creating `TVIVideoView` programmatically
        self.remoteView = TVIVideoView.init(frame: CGRect.zero, delegate: self)
        self.view.insertSubview(self.remoteView, at: 0)

        // `TVIVideoView` supports scaleToFill, scaleAspectFill and scaleAspectFit
        // scaleAspectFit is the default mode when you create `TVIVideoView` programmatically.

        self.remoteView.contentMode = .scaleAspectFit
        remoteView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        remoteView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        remoteView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        remoteView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        view.setNeedsLayout()
    }

Here are the important changes: 以下是重要的更改:

  remoteView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true remoteView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true remoteView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true remoteView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true view.setNeedsLayout() 

But since these changes are so big I'll show you how it fits into a view controller (this compiles) 但是,由于这些更改太大,因此我将向您展示如何将其放入视图控制器(此编译)

import UIKit

class TVIVideoView: UIView {
    var delegate: UIViewController
    init(frame: CGRect, delegate: UIViewController) {
        self.delegate = delegate
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ViewController: UIViewController {
    var remoteView =  UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func setupRemoteVideoView() {
        // Creating `TVIVideoView` programmatically
        self.remoteView = TVIVideoView.init(frame: CGRect.zero, delegate: self)
        self.view.insertSubview(self.remoteView, at: 0)
        // `TVIVideoView` supports scaleToFill, scaleAspectFill and scaleAspectFit
        // scaleAspectFit is the default mode when you create `TVIVideoView` programmatically.
        self.remoteView.contentMode = .scaleAspectFit
        remoteView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        remoteView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        remoteView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        remoteView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        view.setNeedsLayout()
    }
}

Discussion: 讨论:

Do a little research on autolayout. 对自动布局进行一些研究。 (Apple has excellent documentation including a Swift book.) Autolayout has improved a lot since the code provided by Twilio was current. (Apple拥有包括Swift书在内的出色文档。)由于Twilio提供的代码是最新的,因此Autolayout有了很大的改进。 Anchors simplify the process immensely. 锚极大地简化了该过程。 The key things to remember with auto layout are: 自动布局要记住的关键是:

  • the view must be added to a parent 该视图必须添加到父级
  • you must activate constraints 您必须激活约束
  • there must be enough constraints for the system to calculate a height, width and starting point. 系统必须有足够的约束来计算高度,宽度和起点。 (the easy way to remember this is that your constraints are ultimately turned into a frame (CGRect) by the system (记住这一点的简单方法是,系统最终会将您的约束变成一个框架(CGRect)

    -- I'm simplifying a bit, but that's the gist of it. -我在简化一点,但这就是要点。 If any of this is unclear let me know and I'll edit my answer. 如果不清楚,请告诉我,我将编辑答案。 🍀 🍀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM