GromoreDemoAdConsoleView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // GromoreDemoAdConsoleView.m
  3. // ABUDemo
  4. //
  5. // Created by bytedance on 2022/2/11.
  6. // Copyright © 2022 bytedance. All rights reserved.
  7. //
  8. #import "GromoreDemoAdConsoleView.h"
  9. @interface GromoreDemoAdConsoleView ()
  10. @property (nonatomic, strong) UILabel *titleLabel;
  11. @property (nonatomic, strong) UITextView *contentView;
  12. @property (nonatomic, strong) UISwitch *autoScrollButton;
  13. @property (nonatomic, copy) NSString *filterWords;
  14. @end
  15. @implementation GromoreDemoAdConsoleView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setupView];
  19. self.filterWords = [NSString stringWithFormat:@"[%@]", [ABUAdSDKManager SDKVersion]];
  20. }
  21. return self;
  22. }
  23. - (instancetype)init {
  24. CGFloat x = CGRectGetWidth([UIScreen mainScreen].bounds) / 3;
  25. CGFloat y = CGRectGetMaxY([UIScreen mainScreen].bounds) - 300;
  26. CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds) / 3 * 2 - 5;
  27. CGFloat height = 200;
  28. CGRect frame = CGRectMake(x, y, width, height);
  29. if (self = [self initWithFrame:frame]) {}
  30. return self;
  31. }
  32. - (void)setupView {
  33. self.backgroundColor = [UIColor whiteColor];
  34. self.layer.borderColor = RQ_MAIN_COLOR.CGColor;
  35. self.layer.borderWidth = 1.f;
  36. self.layer.cornerRadius = 4.f;
  37. self.layer.masksToBounds = YES;
  38. UILabel *label = [[UILabel alloc] init];
  39. label.text = @" 控制台输出内容(可拖动)";
  40. label.font = RQRegularFont(8);
  41. label.userInteractionEnabled = YES;
  42. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragAction:)];
  43. [label addGestureRecognizer:pan];
  44. self.titleLabel = label;
  45. [self addSubview:label];
  46. _contentView = [[UITextView alloc] init];
  47. _contentView.backgroundColor = [UIColor blackColor];
  48. _contentView.textColor = RQ_MAIN_COLOR;
  49. _contentView.editable = NO;
  50. _contentView.alpha = 1.0f;
  51. [self addSubview:_contentView];
  52. // 自动滚动
  53. _autoScrollButton = [[UISwitch alloc] init];
  54. _autoScrollButton.tintColor = RQ_MAIN_COLOR;
  55. _autoScrollButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
  56. _autoScrollButton.on = YES;
  57. _autoScrollButton.alpha = 1.0f;
  58. [self addSubview:_autoScrollButton];
  59. [self setNeedsLayout];
  60. [self layoutIfNeeded];
  61. super.hidden = YES;
  62. }
  63. - (void)layoutSubviews {
  64. [super layoutSubviews];
  65. CGFloat width = self.frame.size.width;
  66. CGFloat height = self.frame.size.height;
  67. self.titleLabel.frame = CGRectMake(0, 0, width, 20);
  68. self.contentView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), width, height - CGRectGetHeight(self.titleLabel.bounds));
  69. self.autoScrollButton.frame = CGRectMake(width - 30, 2.5f, 30, 30);
  70. }
  71. - (void)dragAction:(UIPanGestureRecognizer *)pan {
  72. CGPoint trans = [pan translationInView:self];
  73. self.frame = CGRectOffset(self.frame, trans.x, trans.y);
  74. [pan setTranslation:CGPointZero inView:self];
  75. }
  76. - (void)setHidden:(BOOL)hidden {
  77. static dispatch_once_t onceToken;
  78. static NSLock *_lock = nil;
  79. dispatch_once(&onceToken, ^{
  80. _lock = [[NSLock alloc] init];
  81. });
  82. [_lock lock];
  83. if (hidden) {
  84. // 动画由大变小
  85. self.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
  86. [UIView animateWithDuration:0.3 animations:^{
  87. self.alpha = 0.1f;
  88. self.transform = CGAffineTransformMake(0.01, 0, 0, 0.01, 0, 0);
  89. } completion:^(BOOL finished) {
  90. self.alpha = 0.f;
  91. self.transform = CGAffineTransformIdentity;
  92. [self stopMonitor];
  93. }];
  94. } else {
  95. // 动画由小变大
  96. self.transform = CGAffineTransformMake(0.0, 0, 0, 0.0, 0, 0);
  97. [UIView animateWithDuration:0.3f animations:^{
  98. self.alpha = 0.7;
  99. self.transform = CGAffineTransformMake(1.f, 0, 0, 1.f, 0, 0);
  100. } completion:^(BOOL finished) {
  101. // 恢复原位
  102. self.transform = CGAffineTransformIdentity;
  103. [self startMonitor];
  104. }];
  105. }
  106. [_lock unlock];
  107. }
  108. - (void)startMonitor {
  109. [[GromoreDemoAdConsoleView _monitors] addObject:self];
  110. }
  111. - (void)stopMonitor {
  112. [[GromoreDemoAdConsoleView _monitors] removeObject:self];
  113. }
  114. - (void)log:(NSString *)string {
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. if (![string containsString:self.filterWords]) return;
  117. self.contentView.text = [self.contentView.text stringByAppendingFormat:@"\n%@",string];
  118. if (!self.autoScrollButton.isOn) return;
  119. NSRange bottom = NSMakeRange(self.contentView.text.length -1, 1);
  120. [self.contentView scrollRangeToVisible:bottom];
  121. });
  122. }
  123. + (NSMutableSet *)_monitors {
  124. static NSMutableSet *_set = nil;
  125. static dispatch_once_t onceToken;
  126. dispatch_once(&onceToken, ^{
  127. _set = [NSMutableSet set];
  128. });
  129. return _set;
  130. }
  131. @end
  132. static void (*Gromore_ORI_NSLog)(NSString *format, ...);
  133. static void Gromore_NSLog(NSString *format, ...) {
  134. va_list list;
  135. va_start(list, format);
  136. NSString *string = [[NSString alloc] initWithFormat:format arguments:list];
  137. if (string) {
  138. [[[GromoreDemoAdConsoleView _monitors] copy] enumerateObjectsUsingBlock:^(GromoreDemoAdConsoleView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  139. [obj log:string];
  140. }];
  141. }
  142. va_end(list);
  143. Gromore_ORI_NSLog(@"%@",string);
  144. }
  145. @implementation GromoreDemoAdConsoleView (HOOK)
  146. // + (void)load {
  147. // if (Gromore_ORI_NSLog == NULL) {
  148. // struct rebinding mybind;
  149. // mybind.name = "NSLog";
  150. // mybind.replacement = Gromore_NSLog;
  151. // mybind.replaced = (void *)&Gromore_ORI_NSLog;
  152. // struct rebinding rebns[] = {mybind};
  153. // rebind_symbols(rebns, 1);
  154. // }
  155. // }
  156. @end