forked from Wtrwx/DYYY
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDYYYFloatSpeedButton.m
More file actions
471 lines (389 loc) · 14.9 KB
/
DYYYFloatSpeedButton.m
File metadata and controls
471 lines (389 loc) · 14.9 KB
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
#import "AwemeHeaders.h"
#import "DYYYFloatClearButton.h"
#import "DYYYFloatSpeedButton.h"
#import "DYYYUtils.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@class AWEFeedCellViewController;
FloatingSpeedButton *speedButton = nil;
BOOL dyyyCommentViewVisible = NO;
BOOL showSpeedX = NO;
CGFloat speedButtonSize = 32.0;
BOOL isFloatSpeedButtonEnabled = NO;
BOOL speedButtonForceHidden = NO;
BOOL dyyyInteractionViewVisible = NO;
static void DYYYApplySpeedButtonHiddenState(UIView *button, BOOL hidden) {
if (!button) {
return;
}
void (^applyBlock)(UIView *) = ^(UIView *target) {
if (!target) {
return;
}
if (target.hidden != hidden) {
target.hidden = hidden;
}
};
if ([NSThread isMainThread]) {
applyBlock(button);
} else {
__weak UIView *weakButton = button;
dispatch_async(dispatch_get_main_queue(), ^{
applyBlock(weakButton);
});
}
}
static BOOL DYYYShouldHideSpeedButton(void) {
BOOL clearModeActive = (hideButton && hideButton.isElementsHidden);
if (clearModeActive) {
BOOL hideSpeedInClearMode = [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYHideSpeed"];
if (hideSpeedInClearMode) {
return YES;
}
return speedButtonForceHidden;
}
if (!dyyyInteractionViewVisible) {
return YES;
}
if (dyyyCommentViewVisible) {
return YES;
}
if (speedButtonForceHidden) {
return YES;
}
return NO;
}
NSArray *getSpeedOptions() {
NSString *speedConfig = [[NSUserDefaults standardUserDefaults] stringForKey:@"DYYYSpeedSettings"] ?: @"1.0,1.25,1.5,2.0";
return [speedConfig componentsSeparatedByString:@","];
}
NSInteger getCurrentSpeedIndex() {
NSInteger index = [[NSUserDefaults standardUserDefaults] integerForKey:@"DYYYCurrentSpeedIndex"];
NSArray *speeds = getSpeedOptions();
if (index >= speeds.count || index < 0) {
index = 0;
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:@"DYYYCurrentSpeedIndex"];
}
return index;
}
float getCurrentSpeed() {
NSArray *speeds = getSpeedOptions();
NSInteger index = getCurrentSpeedIndex();
if (speeds.count == 0)
return 1.0;
float speed = [speeds[index] floatValue];
return speed > 0 ? speed : 1.0;
}
void setCurrentSpeedIndex(NSInteger index) {
NSArray *speeds = getSpeedOptions();
if (speeds.count == 0)
return;
index = index % speeds.count;
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:@"DYYYCurrentSpeedIndex"];
}
void updateSpeedButtonUI() {
if (!speedButton)
return;
float currentSpeed = getCurrentSpeed();
NSString *formattedSpeed;
if (fmodf(currentSpeed, 1.0) == 0) {
// 整数值 (1.0, 2.0) -> "1", "2"
formattedSpeed = [NSString stringWithFormat:@"%.0f", currentSpeed];
} else if (fmodf(currentSpeed * 10, 1.0) == 0) {
// 一位小数 (1.5) -> "1.5"
formattedSpeed = [NSString stringWithFormat:@"%.1f", currentSpeed];
} else {
// 两位小数 (1.25) -> "1.25"
formattedSpeed = [NSString stringWithFormat:@"%.2f", currentSpeed];
}
if (showSpeedX) {
formattedSpeed = [formattedSpeed stringByAppendingString:@"x"];
}
if ([NSThread isMainThread]) {
[speedButton setTitle:formattedSpeed forState:UIControlStateNormal];
} else {
__weak FloatingSpeedButton *weakButton = speedButton;
dispatch_async(dispatch_get_main_queue(), ^{
FloatingSpeedButton *strongButton = weakButton;
if (!strongButton) {
return;
}
[strongButton setTitle:formattedSpeed forState:UIControlStateNormal];
});
}
}
FloatingSpeedButton *getSpeedButton(void) { return speedButton; }
NSArray *findViewControllersInHierarchy(UIViewController *rootViewController) {
if (!rootViewController) {
return @[];
}
NSMutableArray *viewControllers = [NSMutableArray array];
[viewControllers addObject:rootViewController];
for (UIViewController *childVC in rootViewController.childViewControllers) {
[viewControllers addObjectsFromArray:findViewControllersInHierarchy(childVC)];
}
return viewControllers;
}
void showSpeedButton(void) {
speedButtonForceHidden = NO;
updateSpeedButtonVisibility();
}
void hideSpeedButton(void) {
speedButtonForceHidden = YES;
updateSpeedButtonVisibility();
}
void updateSpeedButtonVisibility() {
if (!speedButton || !isFloatSpeedButtonEnabled)
return;
DYYYApplySpeedButtonHiddenState(speedButton, DYYYShouldHideSpeedButton());
}
@implementation FloatingSpeedButton
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.accessibilityLabel = @"DYYYSpeedSwitchButton";
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
self.layer.cornerRadius = frame.size.width / 2;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1.0;
self.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:0.2].CGColor;
[self setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.3] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0, 2);
self.layer.shadowOpacity = 0.2;
self.userInteractionEnabled = YES;
self.isResponding = YES;
self.originalAlpha = 1.0;
self.alpha = 0.5;
[self resetFadeTimer];
[self ensureStatusCheckTimerRunning];
[self setupGestureRecognizers];
[self loadSavedPosition];
self.justToggledLock = NO;
}
return self;
}
- (void)setupGestureRecognizers {
for (UIGestureRecognizer *recognizer in [self.gestureRecognizers copy]) {
[self removeGestureRecognizer:recognizer];
}
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPressGesture.minimumPressDuration = 0.5;
[self addGestureRecognizer:longPressGesture];
[self addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(handleTouchDown:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(handleTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
panGesture.delegate = (id<UIGestureRecognizerDelegate>)self;
longPressGesture.delegate = (id<UIGestureRecognizerDelegate>)self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
- (void)handleTouchDown:(UIButton *)sender {
self.isResponding = YES;
[self resetFadeTimer];
}
- (void)handleTouchUpInside:(UIButton *)sender {
if (self.justToggledLock) {
self.justToggledLock = NO;
return;
}
[self resetFadeTimer];
[UIView animateWithDuration:0.08
animations:^{
self.transform = CGAffineTransformMakeScale(1.15, 1.15);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.08
animations:^{
self.transform = CGAffineTransformIdentity;
}];
}];
if (self.interactionController) {
@try {
[self.interactionController speedButtonTapped:self];
} @catch (NSException *exception) {
self.isResponding = NO;
}
} else {
self.isResponding = NO;
}
}
- (void)handleTouchUpOutside:(UIButton *)sender {
self.justToggledLock = NO;
[self resetFadeTimer];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
self.isResponding = YES;
if (gesture.state == UIGestureRecognizerStateBegan) {
[self resetFadeTimer];
self.originalLockState = self.isLocked;
[self toggleLockState];
}
}
- (void)toggleLockState {
self.isLocked = !self.isLocked;
self.justToggledLock = YES;
NSString *toastMessage = self.isLocked ? @"按钮已锁定" : @"按钮已解锁";
[DYYYUtils showToast:toastMessage];
if (self.isLocked) {
[self saveButtonPosition];
}
if (@available(iOS 10.0, *)) {
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
[generator prepare];
[generator impactOccurred];
}
__weak __typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf.justToggledLock = NO;
});
}
- (void)resetToggleLockFlag {
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf.justToggledLock = NO;
});
}
- (void)resetButtonState {
self.justToggledLock = NO;
self.isResponding = YES;
self.userInteractionEnabled = YES;
self.transform = CGAffineTransformIdentity;
self.alpha = self.originalAlpha;
[self resetFadeTimer];
[self setupGestureRecognizers];
}
- (void)resetFadeTimer {
if (self.fadeTimer) {
[self.fadeTimer invalidate];
self.fadeTimer = nil;
}
__weak __typeof(self) weakSelf = self;
NSTimer *fadeTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
repeats:NO
block:^(NSTimer *timer) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[UIView animateWithDuration:0.3
animations:^{
strongSelf.alpha = 0.5;
}];
strongSelf.fadeTimer = nil;
}];
self.fadeTimer = fadeTimer;
if (self.alpha != self.originalAlpha) {
[UIView animateWithDuration:0.2
animations:^{
self.alpha = self.originalAlpha;
}];
}
}
- (void)handlePan:(UIPanGestureRecognizer *)pan {
if (self.isLocked)
return;
self.justToggledLock = NO;
[self resetFadeTimer];
CGPoint touchPoint = [pan locationInView:self.superview];
if (pan.state == UIGestureRecognizerStateBegan) {
self.lastLocation = self.center;
} else if (pan.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [pan 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;
[pan setTranslation:CGPointZero inView:self.superview];
self.alpha = 0.8;
} else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
self.alpha = self.originalAlpha;
[self saveButtonPosition];
}
}
- (void)saveButtonPosition {
if (self.superview) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:self.center.x / self.superview.bounds.size.width forKey:@"DYYYSpeedButtonCenterXPercent"];
[defaults setFloat:self.center.y / self.superview.bounds.size.height forKey:@"DYYYSpeedButtonCenterYPercent"];
[defaults setBool:self.isLocked forKey:@"DYYYSpeedButtonLocked"];
}
}
- (void)loadSavedPosition {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
float centerXPercent = [defaults floatForKey:@"DYYYSpeedButtonCenterXPercent"];
float centerYPercent = [defaults floatForKey:@"DYYYSpeedButtonCenterYPercent"];
self.isLocked = [defaults boolForKey:@"DYYYSpeedButtonLocked"];
if (centerXPercent > 0 && centerYPercent > 0 && self.superview) {
self.center = CGPointMake(centerXPercent * self.superview.bounds.size.width, centerYPercent * self.superview.bounds.size.height);
}
}
- (void)didMoveToWindow {
[super didMoveToWindow];
if (!self.window) {
[self stopTimers];
return;
}
[self ensureStatusCheckTimerRunning];
[self resetFadeTimer];
}
- (void)ensureStatusCheckTimerRunning {
if (self.statusCheckTimer && [self.statusCheckTimer isValid]) {
return;
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAndRecoverButtonStatus) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.statusCheckTimer = timer;
}
- (void)stopTimers {
if (self.statusCheckTimer) {
[self.statusCheckTimer invalidate];
self.statusCheckTimer = nil;
}
if (self.fadeTimer) {
[self.fadeTimer invalidate];
self.fadeTimer = nil;
}
}
- (void)checkAndRecoverButtonStatus {
if (!self.isResponding) {
[self resetButtonState];
[self setupGestureRecognizers];
self.isResponding = YES;
}
if (!self.interactionController) {
UIWindow *win = [DYYYUtils getActiveWindow];
UIViewController *topVC = win.rootViewController;
while (topVC && topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
if (topVC) {
Class PlayVCClass = NSClassFromString(@"AWEPlayInteractionViewController");
for (UIViewController *vc in findViewControllersInHierarchy(topVC)) {
if (PlayVCClass && [vc isKindOfClass:PlayVCClass]) {
self.interactionController = vc;
break;
}
}
}
}
}
- (void)dealloc {
[self stopTimers];
}
@end