-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatWindow.h
185 lines (147 loc) · 5.91 KB
/
FloatWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// FloatWindow.h
// h5gg
//
// Created by admin on 24/4/2022.
//
#ifndef FloatWindow_h
#define FloatWindow_h
@interface AppWinController : UIViewController
@property UIViewController* bindVC;
@end
@implementation AppWinController
-(instancetype)initWithBind:(UIViewController*)vc {
self = [super init];
if(self) {
self.bindVC = vc;
}
return self;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
NSLog(@"FloatWindow %@ %@", NSStringFromSelector(_cmd) ,NSStringFromSelector(aSelector));
return self.bindVC;
}
- (BOOL)shouldAutorotate {
if(!PGVSharedData->followCurrentOrientation)
return self.bindVC.shouldAutorotate;
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if(!PGVSharedData->followCurrentOrientation)
return self.bindVC.supportedInterfaceOrientations;
return (UIInterfaceOrientationMask)(1<<PGVSharedData->curOrientation);
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if(!PGVSharedData->followCurrentOrientation)
return self.bindVC.preferredInterfaceOrientationForPresentation;
return (UIInterfaceOrientation)PGVSharedData->curOrientation;
}
@end
@interface FloatController : UIViewController
//@property UIWindow* followWindow;
@property UIInterfaceOrientationMask followOrientationMask;
@property void (^onResizeCallback)(CGSize size);
@end
@implementation FloatController
-(instancetype)init {
self = [super init];
if(self) {
//self.followWindow = UIApplication.sharedApplication.keyWindow;
self.followOrientationMask = UIApplication.sharedApplication.keyWindow.rootViewController.supportedInterfaceOrientations;
}
return self;
}
//如果不定义旋转相关委托函数, 并且屏幕锁定开关没有打开, 则UIAlertController会跟随陀螺仪旋转, 并且界面全部卡死
//主要是supportedInterfaceOrientations返回的支持方向集合, 如果原window不支持竖屏, 新window旋转为横屏, 则原window会卡死
//当前keyWindow是不受控制的, 即使没有makeKeyAndVisible, 也可能会成为keyWindow, 可能是windows发生变化时, 系统自动设置了
- (BOOL)shouldAutorotate { //ipad这没调用?
BOOL should;
if(PGVSharedData->enable && PGVSharedData->viewHosted && PGVSharedData->followCurrentOrientation)
should = YES;
else
should = YES; //self.followWindow.rootViewController.shouldAutorotate;
dumpKeyWindow("FloatWindow shouldAutorotate");
return should;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
UIInterfaceOrientationMask mask;
if(PGVSharedData->enable && PGVSharedData->viewHosted && PGVSharedData->followCurrentOrientation)
mask = (UIInterfaceOrientationMask)(1<<PGVSharedData->curOrientation);
else {
uint64_t mask2 = 1<<UIApplication.sharedApplication.statusBarOrientation;
mask = self.followOrientationMask | mask2;
}
dumpKeyWindow("FloatWindow supportedOrientations");
return mask;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation prefedrred;
if(PGVSharedData->enable && PGVSharedData->viewHosted && PGVSharedData->followCurrentOrientation)
prefedrred = (UIInterfaceOrientation)PGVSharedData->curOrientation;
else
prefedrred = UIApplication.sharedApplication.statusBarOrientation; //self.followWindow.rootViewController.preferredInterfaceOrientationForPresentation;
dumpKeyWindow("FloatWindow preferredOrientation");
return prefedrred;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
NSLog(@"FloatWindow=resize=%f,%f : %@", size.width, size.height, self.view);
if(self.onResizeCallback) self.onResizeCallback(size);
}
@end
@interface FloatWindow : UIWindow
@end
@implementation FloatWindow
// recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
-(nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event
{
UIView* v = [super hitTest:point withEvent:event];
//NSLog(@"touchtest floatwin hitTest=%@, %@\n%@", NSStringFromCGPoint(point), event, v);
return v;
}
// default returns YES if point is in bounds
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;
{
//NSLog(@"touchtest floatwin pointInside=%@, %@", NSStringFromCGPoint(point), event);
int count = (int)self.subviews.count;
for (int i = count - 1; i >= 0;i-- ) {
UIView *childV = self.subviews[i];
// 把当前坐标系上的点转换成子控件坐标系上的点.
CGPoint childP = [self convertPoint:point toView:childV];
UIView *fitView = [childV hitTest:childP withEvent:event];
if(fitView) {
//NSLog(@"FloatWindow pointInside=%@", fitView);
return YES;
}
}
return NO;
}
-(void)setHidden:(BOOL)hidden
{
NSLog(@"FloatWindow setHidden=%d", hidden);
if(hidden==NO)
{
// UIWindow* keyWindow = UIApplication.sharedApplication.keyWindow;
// NSLog(@"FloatWindow follow=%@", keyWindow);
// ((FloatController*)self.rootViewController).followWindow = keyWindow;
((FloatController*)self.rootViewController).followOrientationMask = UIApplication.sharedApplication.keyWindow.rootViewController.supportedInterfaceOrientations;
dumpKeyWindow("FloatWindow show");
}
[super setHidden:hidden];
if(hidden==NO)
{
//移除vc自动创建的全屏view (ipad模式还会有多层superview(在window出来之后才会存在))
UIView* superview = self.rootViewController.view;
while(superview && ![superview isKindOfClass:UIWindow.class])
{
[superview setHidden:YES];
superview = superview.superview;
}
}
}
@end
#endif /* FloatWindow_h */