123456789101112131415161718192021222324252627282930313233343536373839404142 |
- //
- // ZFOrientationObserver+RQExtension.m
- // jiaPei
- //
- // Created by 张嵘 on 2020/4/3.
- // Copyright © 2020 JCZ. All rights reserved.
- //
- #import "ZFOrientationObserver+RQExtension.h"
- #import <objc/runtime.h>
- @interface ZFFullViewController : UIViewController
- @end
- @implementation ZFFullViewController (RQExtension)
- + (void)load {
- // 方法交换,为的是当ZFFullViewController调用shouldAutorotate时候,调用的是我们的my_shouldAutorotate方法
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- Class class = [self class];
- SEL originalSelector = @selector(shouldAutorotate);
- SEL swizzledSelector = @selector(my_shouldAutorotate);
- Method originalMethod = class_getInstanceMethod(class, originalSelector);
- Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
- BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
- if (success) {
- class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
- } else {
- method_exchangeImplementations(originalMethod, swizzledMethod);
- }
- });
- }
- - (BOOL)my_shouldAutorotate {
- [self my_shouldAutorotate]; // 由于方法交换,实际上调用的是ZFFullViewController的shouldAutorotate
- return NO;
- }
- @end
- @implementation ZFOrientationObserver (RQExtension)
- @end
|