Skip to content

Commit 561b3fd

Browse files
committed
Merge branch 'master' into dev/3.2.0
2 parents da3222a + 12e2561 commit 561b3fd

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

RtcSurfaceView.swift

+28-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@ import UIKit
1111
import AgoraRtcKit
1212

1313
class RtcSurfaceView: UIView {
14-
private lazy var canvas: AgoraRtcVideoCanvas = {
15-
var canvas = AgoraRtcVideoCanvas()
16-
canvas.view = self
17-
return canvas
18-
}()
14+
private var surface: UIView
15+
private var canvas: AgoraRtcVideoCanvas
1916
private weak var channel: AgoraRtcChannel?
20-
21-
func destroy() {
17+
18+
override init(frame: CGRect) {
19+
surface = UIView(frame: frame)
20+
canvas = AgoraRtcVideoCanvas()
21+
canvas.view = surface
22+
super.init(frame: frame)
23+
addSubview(surface)
24+
addObserver(self, forKeyPath: "frame", options: .new, context: nil)
25+
}
26+
27+
required init?(coder: NSCoder) {
28+
fatalError("init(coder:) has not been implemented")
29+
}
30+
31+
deinit {
2232
canvas.view = nil
33+
removeObserver(self, forKeyPath: "frame", context: nil)
2334
}
2435

2536
func setData(_ engine: AgoraRtcEngineKit, _ channel: AgoraRtcChannel?, _ uid: Int) {
@@ -45,6 +56,10 @@ class RtcSurfaceView: UIView {
4556
}
4657

4758
private func setupVideoCanvas(_ engine: AgoraRtcEngineKit) {
59+
subviews.forEach { $0.removeFromSuperview() }
60+
surface = UIView(frame: self.frame)
61+
addSubview(surface)
62+
canvas.view = surface
4863
if canvas.uid == 0 {
4964
engine.setupLocalVideo(canvas)
5065
} else {
@@ -73,4 +88,10 @@ class RtcSurfaceView: UIView {
7388
}
7489
}
7590
}
91+
92+
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
93+
if keyPath == "frame" {
94+
surface.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
95+
}
96+
}
7697
}

0 commit comments

Comments
 (0)