Skip to content

Commit

Permalink
fix: use the better way to fix rendering problems
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Dec 18, 2020
1 parent 56ab25b commit a290e73
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions RtcSurfaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ class RtcSurfaceView: UIView {
private var surface: UIView
private var canvas: AgoraRtcVideoCanvas
private weak var channel: AgoraRtcChannel?

override init(frame: CGRect) {
surface = UIView(frame: frame)
surface = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: frame.size))
canvas = AgoraRtcVideoCanvas()
canvas.view = surface
super.init(frame: frame)
addSubview(surface)
addObserver(self, forKeyPath: "frame", options: .new, context: nil)
addObserver(self, forKeyPath: observerForKeyPath(), options: .new, context: nil)
}

func observerForKeyPath() -> String {
return "frame"
}

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

deinit {
canvas.view = nil
removeObserver(self, forKeyPath: "frame", context: nil)
removeObserver(self, forKeyPath: observerForKeyPath(), context: nil)
}

func setData(_ engine: AgoraRtcEngineKit, _ channel: AgoraRtcChannel?, _ uid: Int) {
Expand All @@ -56,8 +60,10 @@ class RtcSurfaceView: UIView {
}

private func setupVideoCanvas(_ engine: AgoraRtcEngineKit) {
subviews.forEach { $0.removeFromSuperview() }
surface = UIView(frame: self.frame)
subviews.forEach {
$0.removeFromSuperview()
}
surface = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size))
addSubview(surface)
canvas.view = surface
if canvas.uid == 0 {
Expand Down Expand Up @@ -88,10 +94,12 @@ class RtcSurfaceView: UIView {
}
}
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "frame" {
surface.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == observerForKeyPath() {
if let rect = change?[.newKey] as? CGRect {
surface.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: rect.size)
}
}
}
}

0 comments on commit a290e73

Please sign in to comment.