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

时间:2018-04-25 11:25:47

标签: ios video twilio swift4.1

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

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

1 个答案:

答案 0 :(得分:-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()
    }
}

讨论:

对autolayout做一点研究。 (Apple有很好的文档,包括一本Swift书。)由于Twilio提供的代码是最新的,因此Autolayout已经有了很大的改进。锚点极大地简化了过程。自动布局要记住的关键事项是:

  • 必须将视图添加到父级
  • 您必须激活约束
  • 系统必须有足够的约束来计算高度,宽度和 初始点。 (记住这个的简单方法就是你的 约束最终由系统转换为框架(CGRect)

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