This repository was archived by the owner on Mar 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathDYYYSettingViewController.m
517 lines (431 loc) · 27.7 KB
/
DYYYSettingViewController.m
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#import "DYYYSettingViewController.h"
typedef NS_ENUM(NSInteger, DYYYSettingItemType) {
DYYYSettingItemTypeSwitch,
DYYYSettingItemTypeTextField,
DYYYSettingItemTypeSpeedPicker
};
@interface DYYYSettingItem : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *key;
@property (nonatomic, assign) DYYYSettingItemType type;
@property (nonatomic, copy, nullable) NSString *placeholder;
+ (instancetype)itemWithTitle:(NSString *)title key:(NSString *)key type:(DYYYSettingItemType)type;
+ (instancetype)itemWithTitle:(NSString *)title key:(NSString *)key type:(DYYYSettingItemType)type placeholder:(nullable NSString *)placeholder;
@end
@implementation DYYYSettingItem
+ (instancetype)itemWithTitle:(NSString *)title key:(NSString *)key type:(DYYYSettingItemType)type {
return [self itemWithTitle:title key:key type:type placeholder:nil];
}
+ (instancetype)itemWithTitle:(NSString *)title key:(NSString *)key type:(DYYYSettingItemType)type placeholder:(nullable NSString *)placeholder {
DYYYSettingItem *item = [[DYYYSettingItem alloc] init];
item.title = title;
item.key = key;
item.type = type;
item.placeholder = placeholder;
return item;
}
@end
@interface DYYYSettingViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<NSArray<DYYYSettingItem *> *> *settingSections;
@property (nonatomic, strong) UILabel *footerLabel;
@property (nonatomic, strong) NSMutableArray<NSString *> *sectionTitles;
@property (nonatomic, strong) NSMutableSet *expandedSections;
@property (nonatomic, strong) UIVisualEffectView *blurEffectView;
@property (nonatomic, strong) UIVisualEffectView *vibrancyEffectView;
@property (nonatomic, assign) BOOL isAgreementShown;
@end
@implementation DYYYSettingViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"DYYY设置";
self.expandedSections = [NSMutableSet set];
self.isAgreementShown = NO;
[self setupAppearance];
[self setupBlurEffect];
[self setupTableView];
[self setupSettingItems];
[self setupSectionTitles];
[self setupFooterLabel];
[self addTitleGradientAnimation];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!self.isAgreementShown) {
[self checkFirstLaunch];
self.isAgreementShown = YES;
}
}
- (void)setupAppearance {
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = YES;
}
- (void)setupBlurEffect {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
self.blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.blurEffectView.frame = self.view.bounds;
self.blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.blurEffectView];
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
self.vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
self.vibrancyEffectView.frame = self.blurEffectView.bounds;
self.vibrancyEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.blurEffectView.contentView addSubview:self.vibrancyEffectView];
UIView *overlayView = [[UIView alloc] initWithFrame:self.view.bounds];
overlayView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:overlayView];
}
- (void)setupTableView {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
self.tableView.sectionHeaderTopPadding = 0;
[self.view addSubview:self.tableView];
}
- (void)setupSettingItems {
self.settingSections = @[
@[
[DYYYSettingItem itemWithTitle:@"启用弹幕改色" key:@"DYYYEnableDanmuColor" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"自定弹幕颜色" key:@"DYYYdanmuColor" type:DYYYSettingItemTypeTextField placeholder:@"十六进制"],
[DYYYSettingItem itemWithTitle:@"启用深色键盘" key:@"DYYYisDarkKeyBoard" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"显示视频进度" key:@"DYYYisShowScheduleDisplay" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用自动播放" key:@"DYYYisEnableAutoPlay" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用过滤直播" key:@"DYYYisSkipLive" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用首页净化" key:@"DYYYisEnablePure" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用首页全屏" key:@"DYYYisEnableFullScreen" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用屏蔽广告" key:@"DYYYNoAds" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"评论区毛玻璃" key:@"DYYYisEnableCommentBlur" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"时间属地显示" key:@"DYYYisEnableArea" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"时间标签颜色" key:@"DYYYLabelColor" type:DYYYSettingItemTypeTextField placeholder:@"十六进制"],
[DYYYSettingItem itemWithTitle:@"隐藏系统顶栏" key:@"DYYYisHideStatusbar" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"关注二次确认" key:@"DYYYfollowTips" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"收藏二次确认" key:@"DYYYcollectTips" type:DYYYSettingItemTypeSwitch]
],
@[
[DYYYSettingItem itemWithTitle:@"设置顶栏透明" key:@"DYYYtopbartransparent" type:DYYYSettingItemTypeTextField placeholder:@"0-1小数"],
[DYYYSettingItem itemWithTitle:@"设置全局透明" key:@"DYYYGlobalTransparency" type:DYYYSettingItemTypeTextField placeholder:@"0-1的小数"],
[DYYYSettingItem itemWithTitle:@"设置默认倍速" key:@"DYYYDefaultSpeed" type:DYYYSettingItemTypeSpeedPicker],
[DYYYSettingItem itemWithTitle:@"右侧栏缩放度" key:@"DYYYElementScale" type:DYYYSettingItemTypeTextField placeholder:@"不填默认"],
[DYYYSettingItem itemWithTitle:@"设置首页标题" key:@"DYYYIndexTitle" type:DYYYSettingItemTypeTextField placeholder:@"不填默认"],
[DYYYSettingItem itemWithTitle:@"设置朋友标题" key:@"DYYYFriendsTitle" type:DYYYSettingItemTypeTextField placeholder:@"不填默认"],
[DYYYSettingItem itemWithTitle:@"设置消息标题" key:@"DYYYMsgTitle" type:DYYYSettingItemTypeTextField placeholder:@"不填默认"],
[DYYYSettingItem itemWithTitle:@"设置我的标题" key:@"DYYYSelfTitle" type:DYYYSettingItemTypeTextField placeholder:@"不填默认"]
],
@[
[DYYYSettingItem itemWithTitle:@"隐藏全屏观看" key:@"DYYYisHiddenEntry" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏商城" key:@"DYYYHideShopButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏信息" key:@"DYYYHideMessageButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏朋友" key:@"DYYYHideFriendsButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏加号" key:@"DYYYisHiddenJia" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏红点" key:@"DYYYisHiddenBottomDot" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏底栏背景" key:@"DYYYisHiddenBottomBg" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏侧栏红点" key:@"DYYYisHiddenSidebarDot" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏点赞按钮" key:@"DYYYHideLikeButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏评论按钮" key:@"DYYYHideCommentButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏收藏按钮" key:@"DYYYHideCollectButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏头像按钮" key:@"DYYYHideAvatarButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏音乐按钮" key:@"DYYYHideMusicButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏分享按钮" key:@"DYYYHideShareButton" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏视频定位" key:@"DYYYHideLocation" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏右上搜索" key:@"DYYYHideDiscover" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏相关搜索" key:@"DYYYHideInteractionSearch" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏头像列表" key:@"DYYYisHiddenAvatarList" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏左侧边栏" key:@"DYYYisHiddenLeftSideBar" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏去汽水听" key:@"DYYYHideQuqishuiting" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏热点提示" key:@"DYYYHideHotspot" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏关注直播" key:@"DYYYHidenCapsuleView" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"隐藏顶栏横线" key:@"DYYYHidentopbarprompt" type:DYYYSettingItemTypeSwitch],
],
@[
[DYYYSettingItem itemWithTitle:@"移除推荐" key:@"DYYYHideHotContainer" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除关注" key:@"DYYYHideFollow" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除精选" key:@"DYYYHideMediumVideo" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除商城" key:@"DYYYHideMall" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除朋友" key:@"DYYYHideFriend" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除同城" key:@"DYYYHideNearby" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除团购" key:@"DYYYHideGroupon" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除直播" key:@"DYYYHideTabLive" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除热点" key:@"DYYYHidePadHot" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除经验" key:@"DYYYHideHangout" type:DYYYSettingItemTypeSwitch]
],
@[
[DYYYSettingItem itemWithTitle:@"长按面板复制功能" key:@"DYYYCopyText" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"长按面板保存媒体" key:@"DYYYLongPressDownload" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除评论实况水印" key:@"DYYYCommentLivePhotoNotWaterMark" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"移除评论图片水印" key:@"DYYYCommentNotWaterMark" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"启用双击打开评论" key:@"DYYYEnableDoubleOpenComment" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"保存评论区表情包" key:@"DYYYFourceDownloadEmotion" type:DYYYSettingItemTypeSwitch],
[DYYYSettingItem itemWithTitle:@"长按评论复制文案" key:@"DYYYCommentCopyText" type:DYYYSettingItemTypeSwitch]
]
];
}
- (void)setupSectionTitles {
self.sectionTitles = [@[@"基本设置", @"界面设置", @"隐藏设置", @"顶栏移除", @"功能设置"] mutableCopy];
}
- (void)setupFooterLabel {
self.footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
self.footerLabel.text = [NSString stringWithFormat:@"Developer By @huamidev\nVersion: %@ (%@)", @"2.1-7", @"2503End"];
self.footerLabel.textAlignment = NSTextAlignmentCenter;
self.footerLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
self.footerLabel.textColor = [UIColor colorWithRed:173/255.0 green:216/255.0 blue:230/255.0 alpha:1.0];
self.footerLabel.numberOfLines = 2;
self.footerLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.tableView.tableFooterView = self.footerLabel;
}
- (void)addTitleGradientAnimation {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = @[(__bridge id)[UIColor systemRedColor].CGColor, (__bridge id)[UIColor systemBlueColor].CGColor];
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 0);
gradient.frame = CGRectMake(0, 0, 150, 30);
UIView *titleView = [[UIView alloc] initWithFrame:gradient.frame];
[titleView.layer addSublayer:gradient];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleView.bounds];
titleLabel.text = self.title;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.textColor = [UIColor clearColor];
gradient.mask = titleLabel.layer;
self.navigationItem.titleView = titleView;
CABasicAnimation *colorChange = [CABasicAnimation animationWithKeyPath:@"colors"];
colorChange.toValue = @[(__bridge id)[UIColor systemYellowColor].CGColor, (__bridge id)[UIColor systemGreenColor].CGColor];
colorChange.duration = 2.0;
colorChange.autoreverses = YES;
colorChange.repeatCount = HUGE_VALF;
[gradient addAnimation:colorChange forKey:@"colorChangeAnimation"];
}
#pragma mark - First Launch Agreement
- (void)checkFirstLaunch {
BOOL hasAgreed = [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYUserAgreementAccepted"];
if (!hasAgreed) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAgreementAlert];
});
}
}
- (void)showAgreementAlert {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"用户协议"
message:@"本插件为开源项目\n仅供学习交流用途\n如有侵权请联系, GitHub 仓库:huami1314/DYYY\n请遵守当地法律法规, 逆向工程仅为学习目的\n盗用源码进行商业用途/发布但未标记开源项目必究\n详情请参阅项目内 MIT 许可证\n\n请输入\"我已阅读并同意继续使用\"以继续使用"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alertController.textFields.firstObject;
NSString *inputText = textField.text;
if ([inputText isEqualToString:@"我已阅读并同意继续使用"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DYYYUserAgreementAccepted"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"输入错误"
message:@"请正确输入"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self showAgreementAlert];
}];
[errorAlert addAction:okAction];
[self presentViewController:errorAlert animated:YES completion:nil];
}
}];
UIAlertAction *exitAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
exit(0);
}];
[alertController addAction:confirmAction];
[alertController addAction:exitAction];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.settingSections.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return @"基本设置";
case 1:
return @"界面设置";
case 2:
return @"隐藏设置";
case 3:
return @"顶栏移除";
case 4:
return @"功能设置";
default:
return @"";
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, headerView.bounds.size.width - 50, 44)];
titleLabel.text = [self tableView:tableView titleForHeaderInSection:section];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
[headerView addSubview:titleLabel];
UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width - 30, 15, 14, 14)];
arrowImageView.image = [UIImage systemImageNamed:[self.expandedSections containsObject:@(section)] ? @"chevron.down" : @"chevron.right"];
arrowImageView.tintColor = [UIColor lightGrayColor];
arrowImageView.tag = 100;
arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
[headerView addSubview:arrowImageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = headerView.bounds;
button.tag = section;
[button addTarget:self action:@selector(headerTapped:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.expandedSections containsObject:@(section)] ? self.settingSections[section].count : 0;
}
- (void)toggleSection:(UIButton *)sender {
NSNumber *section = @(sender.tag);
if ([self.expandedSections containsObject:section]) {
[self.expandedSections removeObject:section];
} else {
[self.expandedSections addObject:section];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationFade];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DYYYSettingItem *item = self.settingSections[indexPath.section][indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingCell"];
cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[cell.textLabel.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:16].active = YES;
[cell.textLabel.centerYAnchor constraintEqualToAnchor:cell.contentView.centerYAnchor].active = YES;
}
cell.textLabel.text = item.title;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
cell.backgroundView = nil;
if (indexPath.row == [self.settingSections[indexPath.section] count] - 1) {
cell.layer.cornerRadius = 10;
cell.layer.maskedCorners = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
cell.layer.masksToBounds = YES;
} else {
cell.layer.cornerRadius = 0;
cell.layer.maskedCorners = 0;
}
if (item.type == DYYYSettingItemTypeSwitch) {
UISwitch *switchView = [[UISwitch alloc] init];
[switchView setOn:[[NSUserDefaults standardUserDefaults] boolForKey:item.key]];
[switchView addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
switchView.tag = indexPath.section * 1000 + indexPath.row;
cell.accessoryView = switchView;
} else if (item.type == DYYYSettingItemTypeTextField) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = item.placeholder;
textField.attributedPlaceholder = [[NSAttributedString alloc]
initWithString:item.placeholder
attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:item.key];
textField.textAlignment = NSTextAlignmentRight;
textField.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
textField.textColor = [UIColor whiteColor];
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingDidEnd];
textField.tag = indexPath.section * 1000 + indexPath.row;
cell.accessoryView = textField;
} else if (item.type == DYYYSettingItemTypeSpeedPicker) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UITextField *speedField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
speedField.text = [NSString stringWithFormat:@"%.2f", [[NSUserDefaults standardUserDefaults] floatForKey:@"DYYYDefaultSpeed"]];
speedField.textColor = [UIColor whiteColor];
speedField.borderStyle = UITextBorderStyleNone;
speedField.backgroundColor = [UIColor clearColor];
speedField.textAlignment = NSTextAlignmentRight;
speedField.enabled = NO;
speedField.tag = 999;
cell.accessoryView = speedField;
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat sectionInset = 16;
cell.contentView.frame = UIEdgeInsetsInsetRect(cell.contentView.frame, UIEdgeInsetsMake(0, sectionInset, 0, sectionInset));
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DYYYSettingItem *item = self.settingSections[indexPath.section][indexPath.row];
if (item.type == DYYYSettingItemTypeSpeedPicker) {
[self showSpeedPicker];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)showSpeedPicker {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择倍速"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
NSArray *speeds = @[@0.75, @1.0, @1.25, @1.5, @2.0, @2.5, @3.0];
for (NSNumber *speed in speeds) {
UIAlertAction *action = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%.2f", speed.floatValue]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[[NSUserDefaults standardUserDefaults] setFloat:speed.floatValue forKey:@"DYYYDefaultSpeed"];
[[NSUserDefaults standardUserDefaults] synchronize];
for (NSInteger section = 0; section < self.settingSections.count; section++) {
NSArray *items = self.settingSections[section];
for (NSInteger row = 0; row < items.count; row++) {
DYYYSettingItem *item = items[row];
if (item.type == DYYYSettingItemTypeSpeedPicker) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UITextField *speedField = [cell.accessoryView viewWithTag:999];
if (speedField) {
speedField.text = [NSString stringWithFormat:@"%.2f", speed.floatValue];
}
break;
}
}
}
}];
[alert addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
alert.popoverPresentationController.sourceView = selectedCell;
alert.popoverPresentationController.sourceRect = selectedCell.bounds;
}
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - Actions
- (void)switchToggled:(UISwitch *)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag % 1000 inSection:sender.tag / 1000];
DYYYSettingItem *item = self.settingSections[indexPath.section][indexPath.row];
[[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:item.key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)textFieldDidChange:(UITextField *)textField {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag % 1000 inSection:textField.tag / 1000];
DYYYSettingItem *item = self.settingSections[indexPath.section][indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:item.key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)headerTapped:(UIButton *)sender {
NSNumber *section = @(sender.tag);
if ([self.expandedSections containsObject:section]) {
[self.expandedSections removeObject:section];
} else {
[self.expandedSections addObject:section];
}
UIView *headerView = [self.tableView headerViewForSection:sender.tag];
UIImageView *arrowImageView = [headerView viewWithTag:100];
[UIView animateWithDuration:0.3 animations:^{
arrowImageView.image = [UIImage systemImageNamed:[self.expandedSections containsObject:section] ? @"chevron.down" : @"chevron.right"];
}];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationFade];
}
@end