1

我正在使用 Twilio 进行视频通话,它工作正常,但唯一的问题是无法为远程视频设置全屏。下面的代码是 Twilio 在其QuickStart Project中创建远程视频设置的一种方法。

Xcode 版本:9.3 斯威夫特版本: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)
}
4

1 回答 1

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

以下是重要的变化:

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

但是由于这些变化是如此之大,我将向您展示它如何适合视图控制器(编译)

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

讨论:

对自动布局做一点研究。(Apple 拥有出色的文档,包括一本 Swift 书籍。)自从 Twilio 提供的代码是最新的以来,自动布局已经有了很大的改进。锚极大地简化了这个过程。使用自动布局要记住的关键事项是:

  • 必须将视图添加到父级
  • 你必须激活约束
  • 系统必须有足够的约束来计算高度、宽度和起点。(记住这一点的简单方法是你的约束最终被系统变成了一个框架(CGRect)

    - 我正在简化一点,但这就是它的要点。如果其中任何一个不清楚,请告诉我,我将编辑我的答案。

于 2018-04-25T11:58:18.923 回答