@@ -11,15 +11,26 @@ import UIKit
11
11
import AgoraRtcKit
12
12
13
13
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
19
16
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 {
22
32
canvas. view = nil
33
+ removeObserver ( self , forKeyPath: " frame " , context: nil )
23
34
}
24
35
25
36
func setData( _ engine: AgoraRtcEngineKit , _ channel: AgoraRtcChannel ? , _ uid: Int ) {
@@ -45,6 +56,10 @@ class RtcSurfaceView: UIView {
45
56
}
46
57
47
58
private func setupVideoCanvas( _ engine: AgoraRtcEngineKit ) {
59
+ subviews. forEach { $0. removeFromSuperview ( ) }
60
+ surface = UIView ( frame: self . frame)
61
+ addSubview ( surface)
62
+ canvas. view = surface
48
63
if canvas. uid == 0 {
49
64
engine. setupLocalVideo ( canvas)
50
65
} else {
@@ -73,4 +88,10 @@ class RtcSurfaceView: UIView {
73
88
}
74
89
}
75
90
}
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
+ }
76
97
}
0 commit comments