ZFOrientationObserver+RQExtension.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // ZFOrientationObserver+RQExtension.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2020/4/3.
  6. // Copyright © 2020 JCZ. All rights reserved.
  7. //
  8. #import "ZFOrientationObserver+RQExtension.h"
  9. #import <objc/runtime.h>
  10. @interface ZFFullViewController : UIViewController
  11. @end
  12. @implementation ZFFullViewController (RQExtension)
  13. + (void)load {
  14. // 方法交换,为的是当ZFFullViewController调用shouldAutorotate时候,调用的是我们的my_shouldAutorotate方法
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. Class class = [self class];
  18. SEL originalSelector = @selector(shouldAutorotate);
  19. SEL swizzledSelector = @selector(my_shouldAutorotate);
  20. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  21. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  22. BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  23. if (success) {
  24. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  25. } else {
  26. method_exchangeImplementations(originalMethod, swizzledMethod);
  27. }
  28. });
  29. }
  30. - (BOOL)my_shouldAutorotate {
  31. [self my_shouldAutorotate]; // 由于方法交换,实际上调用的是ZFFullViewController的shouldAutorotate
  32. return NO;
  33. }
  34. @end
  35. @implementation ZFOrientationObserver (RQExtension)
  36. @end