forked from Wtrwx/DYYY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDYYYFloatClearButton.xm
488 lines (480 loc) · 16 KB
/
DYYYFloatClearButton.xm
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Tweak Name: 1KeyHideDYUI
* Target App: com.ss.iphone.ugc.Aweme
* Dev: @c00kiec00k 曲奇的坏品味🍻
* iOS Version: 16.5
*/
#import "DYYYManager.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <signal.h>
// 添加变量跟踪是否在目标视图控制器中
static BOOL isInPlayInteractionVC = NO;
// HideUIButton 接口声明
@interface HideUIButton : UIButton
// 状态属性
@property(nonatomic, assign) BOOL isElementsHidden;
@property(nonatomic, assign) BOOL isLocked;
// UI 相关属性
@property(nonatomic, strong) NSMutableArray *hiddenViewsList;
@property(nonatomic, strong) UIImage *showIcon;
@property(nonatomic, strong) UIImage *hideIcon;
@property(nonatomic, assign) CGFloat originalAlpha;
// 计时器属性
@property(nonatomic, strong) NSTimer *checkTimer;
@property(nonatomic, strong) NSTimer *fadeTimer;
// 方法声明
- (void)resetFadeTimer;
- (void)hideUIElements;
- (void)findAndHideViews:(NSArray *)classNames;
- (void)safeResetState;
- (void)startPeriodicCheck;
- (UIViewController *)findViewController:(UIView *)view;
- (void)loadIcons;
- (void)handlePan:(UIPanGestureRecognizer *)gesture;
- (void)handleTap;
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture;
- (void)handleTouchDown;
- (void)handleTouchUpInside;
- (void)handleTouchUpOutside;
- (void)saveLockState;
- (void)loadLockState;
@end
// 全局变量
static HideUIButton *hideButton;
static BOOL isAppInTransition = NO;
static NSArray *targetClassNames;
static void findViewsOfClassHelper(UIView *view, Class viewClass, NSMutableArray *result) {
if ([view isKindOfClass:viewClass]) {
[result addObject:view];
}
for (UIView *subview in view.subviews) {
findViewsOfClassHelper(subview, viewClass, result);
}
}
static UIWindow *getKeyWindow(void) {
UIWindow *keyWindow = nil;
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
return keyWindow;
}
static void forceResetAllUIElements(void) {
UIWindow *window = getKeyWindow();
if (!window)
return;
for (NSString *className in targetClassNames) {
Class viewClass = NSClassFromString(className);
if (!viewClass)
continue;
NSMutableArray *views = [NSMutableArray array];
findViewsOfClassHelper(window, viewClass, views);
for (UIView *view in views) {
view.alpha = 1.0;
}
}
}
static void reapplyHidingToAllElements(HideUIButton *button) {
if (!button || !button.isElementsHidden)
return;
[button hideUIElements];
}
static void initTargetClassNames(void) {
targetClassNames = @[
@"AWEHPTopBarCTAContainer", @"AWEHPDiscoverFeedEntranceView", @"AWELeftSideBarEntranceView", @"DUXBadge", @"AWEBaseElementView", @"AWEElementStackView",
@"AWEPlayInteractionDescriptionLabel", @"AWEUserNameLabel", @"AWEStoryProgressSlideView", @"AWEStoryProgressContainerView", @"ACCEditTagStickerView", @"AWEFeedTemplateAnchorView",
@"AWESearchFeedTagView", @"AWEPlayInteractionSearchAnchorView", @"AFDRecommendToFriendTagView", @"AWELandscapeFeedEntryView", @"AWEFeedAnchorContainerView", @"AFDAIbumFolioView"
];
}
@implementation HideUIButton
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.layer.cornerRadius = frame.size.width / 2;
self.layer.masksToBounds = YES;
self.isElementsHidden = NO;
self.hiddenViewsList = [NSMutableArray array];
// 设置默认状态为半透明
self.originalAlpha = 1.0; // 交互时为完全不透明
self.alpha = 0.5; // 初始为半透明
// 加载保存的锁定状态
[self loadLockState];
[self loadIcons];
[self setImage:self.showIcon forState:UIControlStateNormal];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self addGestureRecognizer:panGesture];
[self addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(handleTouchDown) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(handleTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(handleTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self addGestureRecognizer:longPressGesture];
[self startPeriodicCheck];
[self resetFadeTimer];
// 初始状态下隐藏按钮,直到进入正确的控制器
self.hidden = YES;
}
return self;
}
- (void)startPeriodicCheck {
[self.checkTimer invalidate];
self.checkTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
repeats:YES
block:^(NSTimer *timer) {
if (self.isElementsHidden) {
[self hideUIElements];
}
}];
}
- (void)resetFadeTimer {
[self.fadeTimer invalidate];
self.fadeTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
repeats:NO
block:^(NSTimer *timer) {
[UIView animateWithDuration:0.3
animations:^{
self.alpha = 0.5; // 变为半透明
}];
}];
// 交互时变为完全不透明
if (self.alpha != self.originalAlpha) {
[UIView animateWithDuration:0.2
animations:^{
self.alpha = self.originalAlpha; // 变为完全不透明
}];
}
}
- (void)saveLockState {
[[NSUserDefaults standardUserDefaults] setBool:self.isLocked forKey:@"DYYYHideUIButtonLockState"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)loadLockState {
self.isLocked = [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYHideUIButtonLockState"];
}
- (void)loadIcons {
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *iconPath = [documentsPath stringByAppendingPathComponent:@"DYYY/qingping.png"];
UIImage *customIcon = [UIImage imageWithContentsOfFile:iconPath];
if (customIcon) {
self.showIcon = customIcon;
self.hideIcon = customIcon;
} else {
[self setTitle:@"隐藏" forState:UIControlStateNormal];
[self setTitle:@"显示" forState:UIControlStateSelected];
self.titleLabel.font = [UIFont systemFontOfSize:10];
}
}
- (void)handleTouchDown {
[self resetFadeTimer]; // 这会使按钮变为完全不透明
}
- (void)handleTouchUpInside {
[self resetFadeTimer]; // 这会使按钮变为完全不透明
}
- (void)handleTouchUpOutside {
[self resetFadeTimer]; // 这会使按钮变为完全不透明
}
- (UIViewController *)findViewController:(UIView *)view {
__weak UIResponder *responder = view;
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = [responder nextResponder];
if (!responder)
break;
}
return nil;
}
- (void)handlePan:(UIPanGestureRecognizer *)gesture {
if (self.isLocked)
return;
[self resetFadeTimer]; // 这会使按钮变为完全不透明
CGPoint translation = [gesture translationInView:self.superview];
CGPoint newCenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
newCenter.x = MAX(self.frame.size.width / 2, MIN(newCenter.x, self.superview.frame.size.width - self.frame.size.width / 2));
newCenter.y = MAX(self.frame.size.height / 2, MIN(newCenter.y, self.superview.frame.size.height - self.frame.size.height / 2));
self.center = newCenter;
[gesture setTranslation:CGPointZero inView:self.superview];
if (gesture.state == UIGestureRecognizerStateEnded) {
[[NSUserDefaults standardUserDefaults] setObject:NSStringFromCGPoint(self.center) forKey:@"DYYYHideUIButtonPosition"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)handleTap {
if (isAppInTransition)
return;
[self resetFadeTimer]; // 这会使按钮变为完全不透明
if (!self.isElementsHidden) {
[self hideUIElements];
self.isElementsHidden = YES;
self.selected = YES;
} else {
forceResetAllUIElements();
self.isElementsHidden = NO;
[self.hiddenViewsList removeAllObjects];
self.selected = NO;
}
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
[self resetFadeTimer]; // 这会使按钮变为完全不透明
self.isLocked = !self.isLocked;
// 保存锁定状态
[self saveLockState];
NSString *toastMessage = self.isLocked ? @"按钮已锁定" : @"按钮已解锁";
[DYYYManager showToast:toastMessage];
if (@available(iOS 10.0, *)) {
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
[generator prepare];
[generator impactOccurred];
}
}
}
- (void)hideUIElements {
[self.hiddenViewsList removeAllObjects];
[self findAndHideViews:targetClassNames];
self.isElementsHidden = YES;
}
- (void)findAndHideViews:(NSArray *)classNames {
for (UIWindow *window in [UIApplication sharedApplication].windows) {
for (NSString *className in classNames) {
Class viewClass = NSClassFromString(className);
if (!viewClass)
continue;
NSMutableArray *views = [NSMutableArray array];
findViewsOfClassHelper(window, viewClass, views);
for (UIView *view in views) {
if ([view isKindOfClass:[UIView class]]) {
if ([view isKindOfClass:NSClassFromString(@"AWELeftSideBarEntranceView")]) {
UIViewController *controller = [self findViewController:view];
if (![controller isKindOfClass:NSClassFromString(@"AWEFeedContainerViewController")]) {
continue;
}
}
[self.hiddenViewsList addObject:view];
view.alpha = 0.0;
}
}
}
}
}
- (void)safeResetState {
forceResetAllUIElements();
self.isElementsHidden = NO;
[self.hiddenViewsList removeAllObjects];
self.selected = NO;
}
- (void)dealloc {
[self.checkTimer invalidate];
[self.fadeTimer invalidate];
self.checkTimer = nil;
self.fadeTimer = nil;
}
@end
// Hook 部分
%hook UIView
- (id)initWithFrame:(CGRect)frame {
UIView *view = %orig;
if (hideButton && hideButton.isElementsHidden) {
for (NSString *className in targetClassNames) {
if ([view isKindOfClass:NSClassFromString(className)]) {
if ([view isKindOfClass:NSClassFromString(@"AWELeftSideBarEntranceView")]) {
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *controller = [hideButton findViewController:view];
if ([controller isKindOfClass:NSClassFromString(@"AWEFeedContainerViewController")]) {
view.alpha = 0.0;
}
});
break;
}
view.alpha = 0.0;
break;
}
}
}
return view;
}
- (void)didAddSubview:(UIView *)subview {
%orig;
if (hideButton && hideButton.isElementsHidden) {
for (NSString *className in targetClassNames) {
if ([subview isKindOfClass:NSClassFromString(className)]) {
if ([subview isKindOfClass:NSClassFromString(@"AWELeftSideBarEntranceView")]) {
UIViewController *controller = [hideButton findViewController:subview];
if ([controller isKindOfClass:NSClassFromString(@"AWEFeedContainerViewController")]) {
subview.alpha = 0.0;
}
break;
}
subview.alpha = 0.0;
break;
}
}
}
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
%orig;
if (hideButton && hideButton.isElementsHidden) {
for (NSString *className in targetClassNames) {
if ([self isKindOfClass:NSClassFromString(className)]) {
if ([self isKindOfClass:NSClassFromString(@"AWELeftSideBarEntranceView")]) {
UIViewController *controller = [hideButton findViewController:self];
if ([controller isKindOfClass:NSClassFromString(@"AWEFeedContainerViewController")]) {
self.alpha = 0.0;
}
break;
}
self.alpha = 0.0;
break;
}
}
}
}
%end
%hook AWEFeedTableViewCell
- (void)prepareForReuse {
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
%orig;
}
- (void)layoutSubviews {
%orig;
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
}
%end
%hook AWEFeedViewCell
- (void)layoutSubviews {
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
%orig;
}
- (void)setModel:(id)model {
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
%orig;
}
%end
%hook UIViewController
- (void)viewWillAppear:(BOOL)animated {
%orig;
isAppInTransition = YES;
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
isAppInTransition = NO;
});
}
- (void)viewWillDisappear:(BOOL)animated {
%orig;
isAppInTransition = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
isAppInTransition = NO;
});
}
%end
// 修改: 使用 viewWillAppear 和 loadView 来更早地显示按钮
%hook AWEPlayInteractionViewController
- (void)loadView {
%orig;
// 提前准备按钮显示
if (hideButton) {
hideButton.hidden = NO;
hideButton.alpha = 0.5;
}
}
- (void)viewWillAppear:(BOOL)animated {
%orig;
isInPlayInteractionVC = YES;
// 立即显示按钮
if (hideButton) {
hideButton.hidden = NO;
hideButton.alpha = 0.5;
}
}
- (void)viewDidAppear:(BOOL)animated {
%orig;
// 再次确保按钮可见
if (hideButton) {
hideButton.hidden = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated {
%orig;
isInPlayInteractionVC = NO;
// 立即隐藏按钮
if (hideButton) {
hideButton.hidden = YES;
}
}
%end
%hook AWEFeedContainerViewController
- (void)aweme:(id)arg1 currentIndexWillChange:(NSInteger)arg2 {
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
%orig;
}
- (void)aweme:(id)arg1 currentIndexDidChange:(NSInteger)arg2 {
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
%orig;
}
- (void)viewWillLayoutSubviews {
%orig;
if (hideButton && hideButton.isElementsHidden) {
[hideButton hideUIElements];
}
}
%end
%hook AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BOOL result = %orig;
initTargetClassNames();
// 立即创建按钮,不使用异步操作
BOOL isEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYEnableFloatClearButton"];
if (isEnabled) {
if (hideButton) {
[hideButton removeFromSuperview];
hideButton = nil;
}
CGFloat buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"DYYYEnableFloatClearButtonSize"] ?: 40.0;
hideButton = [[HideUIButton alloc] initWithFrame:CGRectMake(0, 0, buttonSize, buttonSize)];
hideButton.alpha = 0.5;
NSString *savedPositionString = [[NSUserDefaults standardUserDefaults] objectForKey:@"DYYYHideUIButtonPosition"];
if (savedPositionString) {
hideButton.center = CGPointFromString(savedPositionString);
} else {
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
hideButton.center = CGPointMake(screenWidth - buttonSize/2 - 5, screenHeight / 2);
}
// 初始状态下隐藏按钮
hideButton.hidden = YES;
[getKeyWindow() addSubview:hideButton];
// 添加观察者以确保窗口变化时按钮仍然可见
[[NSNotificationCenter defaultCenter] addObserverForName:UIWindowDidBecomeKeyNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull notification) {
if (isInPlayInteractionVC && hideButton && hideButton.hidden) {
hideButton.hidden = NO;
}
}];
}
return result;
}
%end
%ctor {
signal(SIGSEGV, SIG_IGN);
}