UIViewController+Swizzle.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // UIViewController+Swizzle.m
  3. // NGPageLoadingDemo
  4. //
  5. // Created by ngmmxh on 2018/8/26.
  6. // Copyright © 2018年 ngmmxh. All rights reserved.
  7. //
  8. #import "UIViewController+Swizzle.h"
  9. #import <objc/runtime.h>
  10. static char *viewLoadStartTimeKey = "viewLoadStartTimeKey";
  11. @implementation UIViewController (Swizzle)
  12. -(void)setViewLoadStartTime:(CFAbsoluteTime)viewLoadStartTime{
  13. objc_setAssociatedObject(self, &viewLoadStartTimeKey, @(viewLoadStartTime), OBJC_ASSOCIATION_COPY);
  14. }
  15. -(CFAbsoluteTime)viewLoadStartTime{
  16. return [objc_getAssociatedObject(self, &viewLoadStartTimeKey) doubleValue];
  17. }
  18. + (void)load
  19. {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. SEL origSel = @selector(viewDidAppear:);
  23. SEL swizSel = @selector(swiz_viewDidAppear:);
  24. [UIViewController swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];
  25. SEL vcWillAppearSel=@selector(viewWillAppear:);
  26. SEL swizWillAppearSel=@selector(swiz_viewWillAppear:);
  27. [UIViewController swizzleMethods:[self class] originalSelector:vcWillAppearSel swizzledSelector:swizWillAppearSel];
  28. SEL vcDidLoadSel=@selector(viewDidLoad);
  29. SEL swizDidLoadSel=@selector(swiz_viewDidLoad);
  30. [UIViewController swizzleMethods:[self class] originalSelector:vcDidLoadSel swizzledSelector:swizDidLoadSel];
  31. SEL vcDidDisappearSel=@selector(viewDidDisappear:);
  32. SEL swizDidDisappearSel=@selector(swiz_viewDidDisappear:);
  33. [UIViewController swizzleMethods:[self class] originalSelector:vcDidDisappearSel swizzledSelector:swizDidDisappearSel];
  34. SEL vcWillDisappearSel=@selector(viewWillDisappear:);
  35. SEL swizWillDisappearSel=@selector(swiz_viewWillDisappear:);
  36. [UIViewController swizzleMethods:[self class] originalSelector:vcWillDisappearSel swizzledSelector:swizWillDisappearSel];
  37. });
  38. }
  39. + (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
  40. {
  41. Method origMethod = class_getInstanceMethod(class, origSel);
  42. Method swizMethod = class_getInstanceMethod(class, swizSel);
  43. //class_addMethod will fail if original method already exists
  44. BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
  45. if (didAddMethod) {
  46. class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
  47. } else {
  48. //origMethod and swizMethod already exist
  49. method_exchangeImplementations(origMethod, swizMethod);
  50. }
  51. }
  52. - (void)swiz_viewDidAppear:(BOOL)animated
  53. {
  54. [self swiz_viewDidAppear:animated];
  55. if (self.viewLoadStartTime) {
  56. CFAbsoluteTime linkTime = (CACurrentMediaTime() - self.viewLoadStartTime);
  57. NSLog(@" %f s--------------------ssssss %@:速度: %f s",self.viewLoadStartTime, self.class,linkTime );
  58. self.viewLoadStartTime = 0;
  59. }
  60. }
  61. -(void)swiz_viewWillAppear:(BOOL)animated
  62. {
  63. [self swiz_viewWillAppear:animated];
  64. }
  65. -(void)swiz_viewDidDisappear:(BOOL)animated
  66. {
  67. [self swiz_viewDidDisappear:animated];
  68. }
  69. -(void)swiz_viewWillDisappear:(BOOL)animated
  70. {
  71. [self swiz_viewWillDisappear:animated];
  72. }
  73. -(void)swiz_viewDidLoad
  74. {
  75. self.viewLoadStartTime =CACurrentMediaTime();
  76. NSLog(@" %@swiz_viewDidLoad startTime:%f",self.class, self.viewLoadStartTime );
  77. [self swiz_viewDidLoad];
  78. }
  79. @end