#import "KBKeyboardStressTestVC.h" @interface KBKeyboardStressTestVC () @property(nonatomic, strong) UITextView *textView; @property(nonatomic, strong) UIButton *startButton; @property(nonatomic, strong) UIButton *stopButton; @property(nonatomic, strong) UILabel *statusLabel; @property(nonatomic, assign) NSInteger currentCycle; @property(nonatomic, assign) NSInteger totalCycles; @property(nonatomic, assign) BOOL running; @end @implementation KBKeyboardStressTestVC - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = UIColor.whiteColor; self.title = @"键盘压力测试"; self.totalCycles = 200; [self buildUI]; [self updateStatus]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self stop]; } - (void)buildUI { CGFloat w = UIScreen.mainScreen.bounds.size.width; self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, KB_NAV_TOTAL_HEIGHT + 16, w - 32, 22)]; self.statusLabel.font = [UIFont systemFontOfSize:13]; self.statusLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1]; [self.view addSubview:self.statusLabel]; self.textView = [[UITextView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.statusLabel.frame) + 12, w - 32, 160)]; self.textView.text = @"把系统输入法切到自定义键盘后,点击开始,会反复显示/隐藏键盘。"; self.textView.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.15].CGColor; self.textView.layer.borderWidth = 0.5; self.textView.layer.cornerRadius = 8; [self.view addSubview:self.textView]; CGFloat btnW = (w - 16 * 2 - 12) / 2.0; self.startButton = [UIButton buttonWithType:UIButtonTypeSystem]; self.startButton.frame = CGRectMake(16, CGRectGetMaxY(self.textView.frame) + 16, btnW, 44); self.startButton.layer.cornerRadius = 10; self.startButton.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1]; [self.startButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; [self.startButton setTitle:@"开始" forState:UIControlStateNormal]; [self.startButton addTarget:self action:@selector(onStart) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.startButton]; self.stopButton = [UIButton buttonWithType:UIButtonTypeSystem]; self.stopButton.frame = CGRectMake(CGRectGetMaxX(self.startButton.frame) + 12, CGRectGetMinY(self.startButton.frame), btnW, 44); self.stopButton.layer.cornerRadius = 10; self.stopButton.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.08]; [self.stopButton setTitleColor:[UIColor colorWithWhite:0.15 alpha:1] forState:UIControlStateNormal]; [self.stopButton setTitle:@"停止" forState:UIControlStateNormal]; [self.stopButton addTarget:self action:@selector(onStop) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.stopButton]; UIButton *oneCycleBtn = [UIButton buttonWithType:UIButtonTypeSystem]; oneCycleBtn.frame = CGRectMake(16, CGRectGetMaxY(self.startButton.frame) + 12, w - 32, 44); oneCycleBtn.layer.cornerRadius = 10; oneCycleBtn.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.08]; [oneCycleBtn setTitleColor:[UIColor colorWithWhite:0.15 alpha:1] forState:UIControlStateNormal]; [oneCycleBtn setTitle:@"执行 10 次" forState:UIControlStateNormal]; [oneCycleBtn addTarget:self action:@selector(onRunTen) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:oneCycleBtn]; } - (void)onStart { self.totalCycles = 200; [self start]; } - (void)onRunTen { self.totalCycles = 10; [self start]; } - (void)onStop { [self stop]; } - (void)start { if (self.running) { return; } self.running = YES; self.currentCycle = 0; [self updateStatus]; [self runNextCycle]; } - (void)stop { self.running = NO; [self.textView resignFirstResponder]; [self updateStatus]; } - (void)runNextCycle { if (!self.running) { return; } if (self.currentCycle >= self.totalCycles) { [self stop]; return; } self.currentCycle += 1; [self updateStatus]; __weak typeof(self) weakSelf = self; [self.textView becomeFirstResponder]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.18 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ __strong typeof(weakSelf) self = weakSelf; if (!self || !self.running) { return; } [self.textView resignFirstResponder]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ __strong typeof(weakSelf) self = weakSelf; if (!self) { return; } [self runNextCycle]; }); }); } - (void)updateStatus { NSString *run = self.running ? @"运行中" : @"未运行"; self.statusLabel.text = [NSString stringWithFormat:@"状态:%@ | 进度:%ld/%ld", run, (long)self.currentCycle, (long)self.totalCycles]; self.startButton.enabled = !self.running; self.startButton.alpha = self.running ? 0.5 : 1.0; self.stopButton.enabled = self.running; self.stopButton.alpha = self.running ? 1.0 : 0.5; } @end