FHHFPSIndicator.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // FHHFPSIndicator.m
  3. // FHHFPSIndicator
  4. //
  5. // Created by 002 on 16/6/27.
  6. // Copyright © 2016年 002. All rights reserved.
  7. //
  8. #import "FHHFPSIndicator.h"
  9. #define kScreenWidth ([[UIScreen mainScreen] bounds].size.width)
  10. #define SIZE_fpsLabel CGSizeMake(44, 15)
  11. #define FONT_SIZE_fpsLabel (12)
  12. #define TAG_fpsLabel (110213)
  13. #define TEXTCOLOR_fpsLabel ([UIColor colorWithRed:255 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1.00])
  14. #define PADDING_TOP_fpsLabel (200)
  15. #if TARGET_IPHONE_SIMULATOR // SIMULATOR
  16. #define PADDING_LEFT_fpsLabel (47)
  17. #define PADDING_RIGHT_fpsLabel (9)
  18. #define PADDING_CENTER_fpsLabel (1)
  19. #elif TARGET_OS_IPHONE // iPhone
  20. #define PADDING_LEFT_fpsLabel (36)
  21. #define PADDING_RIGHT_fpsLabel (-3)
  22. #define PADDING_CENTER_fpsLabel (3)
  23. #endif
  24. @interface FHHFPSIndicator ()
  25. {
  26. CADisplayLink *_displayLink;
  27. NSTimeInterval _lastTime;
  28. NSUInteger _count;
  29. }
  30. @property (nonatomic, strong) UILabel *fpsLabel;
  31. @end
  32. @implementation FHHFPSIndicator
  33. + (FHHFPSIndicator *)sharedFPSIndicator {
  34. static dispatch_once_t onceToken;
  35. static FHHFPSIndicator *_instance;
  36. dispatch_once(&onceToken, ^{
  37. _instance = [[FHHFPSIndicator alloc] init];
  38. });
  39. return _instance;
  40. }
  41. - (id)init {
  42. if (self = [super init]) {
  43. _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(p_displayLinkTick:)];
  44. [_displayLink setPaused:YES];
  45. [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  46. // create fpsLabel
  47. _fpsLabel = [[UILabel alloc] init];
  48. self.fpsLabelPosition = FPSIndicatorPositionBottomCenter;
  49. _fpsLabel.tag = TAG_fpsLabel;
  50. // set style for fpsLabel
  51. [self p_configFPSLabel];
  52. [[NSNotificationCenter defaultCenter] addObserver: self
  53. selector: @selector(applicationDidBecomeActiveNotification)
  54. name: UIApplicationDidBecomeActiveNotification
  55. object: nil];
  56. [[NSNotificationCenter defaultCenter] addObserver: self
  57. selector: @selector(applicationWillResignActiveNotification)
  58. name: UIApplicationWillResignActiveNotification
  59. object: nil];
  60. }
  61. return self;
  62. }
  63. /**
  64. You can change the fpsLabel style for your app in this function
  65. */
  66. - (void)p_configFPSLabel {
  67. _fpsLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_fpsLabel];
  68. _fpsLabel.backgroundColor = [UIColor clearColor];
  69. _fpsLabel.textColor = TEXTCOLOR_fpsLabel;
  70. _fpsLabel.textAlignment = NSTextAlignmentCenter;
  71. }
  72. - (void)p_displayLinkTick:(CADisplayLink *)link {
  73. if (_lastTime == 0) {
  74. _lastTime = link.timestamp;
  75. return;
  76. }
  77. _count++;
  78. NSTimeInterval delta = link.timestamp - _lastTime;
  79. if (delta < 1) {
  80. return;
  81. }
  82. _lastTime = link.timestamp;
  83. float fps = _count / delta;
  84. _count = 0;
  85. NSString *text = [NSString stringWithFormat:@"%d FPS",(int)round(fps)];
  86. [_fpsLabel setText: text];
  87. }
  88. - (void)show {
  89. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  90. for (NSUInteger i = 0; i < keyWindow.subviews.count; ++i) {
  91. UIView *view = keyWindow.subviews[keyWindow.subviews.count - 1 - i];
  92. if ([view isKindOfClass:[UILabel class]] && view.tag == TAG_fpsLabel) {
  93. return;
  94. }
  95. }
  96. [_displayLink setPaused:NO];
  97. [keyWindow addSubview:_fpsLabel];
  98. }
  99. - (void)hide {
  100. [_displayLink setPaused:YES];
  101. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  102. for (UIView *label in keyWindow.subviews) {
  103. if ([label isKindOfClass:[UILabel class]]&& label.tag == TAG_fpsLabel) {
  104. [label removeFromSuperview];
  105. return;
  106. }
  107. }
  108. }
  109. - (BOOL)isShowingFps {
  110. if (_fpsLabel.superview != nil) {
  111. return YES;
  112. }
  113. return NO;
  114. }
  115. #pragma mark - notification
  116. - (void)applicationDidBecomeActiveNotification {
  117. [_displayLink setPaused:NO];
  118. }
  119. - (void)applicationWillResignActiveNotification {
  120. [_displayLink setPaused:YES];
  121. }
  122. #pragma mark - setter
  123. - (void)setFpsLabelPosition:(FPSIndicatorPosition)fpsLabelPosition {
  124. _fpsLabelPosition = fpsLabelPosition;
  125. switch (_fpsLabelPosition) {
  126. case FPSIndicatorPositionTopLeft:
  127. _fpsLabel.frame = CGRectMake((kScreenWidth - SIZE_fpsLabel.width) / 2 - PADDING_LEFT_fpsLabel - 1, 2.5, SIZE_fpsLabel.width, SIZE_fpsLabel.height);
  128. break;
  129. case FPSIndicatorPositionTopRight:
  130. _fpsLabel.frame = CGRectMake((kScreenWidth + SIZE_fpsLabel.width) / 2 + (PADDING_RIGHT_fpsLabel) , 2.5, SIZE_fpsLabel.width, SIZE_fpsLabel.height);
  131. break;
  132. case FPSIndicatorPositionBottomCenter:
  133. _fpsLabel.frame = CGRectMake((kScreenWidth - SIZE_fpsLabel.width) / 2 + PADDING_CENTER_fpsLabel, PADDING_TOP_fpsLabel, SIZE_fpsLabel.width, SIZE_fpsLabel.height);
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. - (void)setFpsLabelColor:(UIColor *)color {
  140. if (color == nil) {
  141. _fpsLabel.textColor = TEXTCOLOR_fpsLabel;
  142. } else {
  143. _fpsLabel.textColor = color;
  144. }
  145. }
  146. @end