Authored by 阿达

添加swizzle 交换方法 fix 滚动条延迟 code review by 高强

//
// UINavigation+Pop.h
// HuaBanNew
//
// Created by chengxianghe on 15/12/1.
// Copyright © 2015年 CXH. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol UINavigationControllerShouldPop <NSObject>
- (BOOL)navigationControllerShouldPop:(UINavigationController *)navigationController;
- (BOOL)navigationControllerShouldStartInteractivePopGestureRecognizer:(UINavigationController *)navigationController;
@end
@interface UINavigationController (Pop)<UIGestureRecognizerDelegate>
@end
... ...
//
// UINavigation+Pop.m
// HuaBanNew
//
// Created by chengxianghe on 15/12/1.
// Copyright © 2015年 CXH. All rights reserved.
//
#import "UINavigation+Pop.h"
#import <objc/objc-runtime.h>
static NSString *const kOriginDelegate = @"koriginDelegate";
@implementation UINavigationController (Pop)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);
//customPop_viewDidLoad
SEL originalSelectorViewDidLoad = @selector(viewDidLoad);
SEL swizzledSelectorViewDidLoad = @selector(customPop_viewDidLoad);
[self swizzledClass:class originalSelector:originalSelectorViewDidLoad swizzledSelector:swizzledSelectorViewDidLoad];
//customPop_navigationBar:shouldPopItem:
SEL originalSelector = @selector(navigationBar:shouldPopItem:);
SEL swizzledSelector = @selector(customPop_navigationBar:shouldPopItem:);
[self swizzledClass:class originalSelector:originalSelector swizzledSelector:swizzledSelector];
});
}
+ (void)swizzledClass:(Class)class originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
#pragma mark - Method Swizzling
- (void)customPop_viewDidLoad {
[self customPop_viewDidLoad];
objc_setAssociatedObject(self, [kOriginDelegate UTF8String], self.interactivePopGestureRecognizer.delegate, OBJC_ASSOCIATION_ASSIGN);
// NSLog(@"%@", self.interactivePopGestureRecognizer.delegate);
self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
}
- (BOOL)customPop_navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
UIViewController *vc = self.topViewController;
if (item != vc.navigationItem) {
return true;
}
// NSLog(@"navigationBar shouldPopItem:: %@", self);
if ([vc conformsToProtocol:@protocol(UINavigationControllerShouldPop)]) {
if ([(id<UINavigationControllerShouldPop>)vc navigationControllerShouldPop:self]) {
return [self customPop_navigationBar:navigationBar shouldPopItem:item];
} else {
return false;
}
} else {
return [self customPop_navigationBar:navigationBar shouldPopItem:item];
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// NSLog(@"navigationBar gestureRecognizerShouldBegin: %@", self);
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
UIViewController *vc = self.topViewController;
if ([vc conformsToProtocol:@protocol(UINavigationControllerShouldPop)]) {
if ([(id<UINavigationControllerShouldPop>)vc navigationControllerShouldStartInteractivePopGestureRecognizer:self]) {
return true;
} else {
return false;
}
}
id<UIGestureRecognizerDelegate> originDelegate = objc_getAssociatedObject(self, [kOriginDelegate UTF8String]);
return [originDelegate gestureRecognizerShouldBegin:gestureRecognizer];
} else {
return true;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
id<UIGestureRecognizerDelegate> originDelegate = objc_getAssociatedObject(self, [kOriginDelegate UTF8String]);
return [originDelegate gestureRecognizer:gestureRecognizer shouldReceiveTouch:touch];
}
return true;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
id<UIGestureRecognizerDelegate> originDelegate = objc_getAssociatedObject(self, [kOriginDelegate UTF8String]);
return [originDelegate gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
}
return true;
}
@end
\ No newline at end of file
... ...
... ... @@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>
#import "YHExplorerViewController.h"
#import "UINavigation+Pop.h"
@interface YHExplorerView : UIView
<YHExplorer>
... ... @@ -18,5 +19,8 @@
@property (nonatomic) BOOL enableProgressBar;
- (UIWebView *)webView;
/**
* 移除滚动条
*/
- (void)removeProgress;
@end
... ...
... ... @@ -161,4 +161,10 @@
}
}
//移除滚动条
- (void)removeProgress
{
[self.webViewController removeProgress];
}
@end
... ...
... ... @@ -50,6 +50,10 @@
*/
- (void)unRegisterLinkParser:(id <YHLinkParserDelegate>)parser;
/**
* 移除滚动条
*/
- (void)removeProgress;
@end
@interface YHExplorerViewController : CDVViewController
... ...
... ... @@ -28,26 +28,7 @@ static float kprogressViewRadius = 2.0f;
@implementation YHExplorerViewController
- (BOOL)navigationControllerShouldPop:(UINavigationController *)navigationController {
NSLog(@"%@ 点击\n%s", [self class], __func__);
return YES;
}
- (BOOL)navigationControllerShouldStartInteractivePopGestureRecognizer:(UINavigationController *)navigationController {
NSLog(@"%@ 手势\n%s", [self class], __func__);
return YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.progressView removeFromSuperview];
}
- (void)dealloc
{
[self.webView stopLoading];
... ... @@ -110,6 +91,15 @@ static float kprogressViewRadius = 2.0f;
}
}
/**
* 移除滚动条
*/
- (void)removeProgress
{
[self.progressView removeFromSuperview];
}
#pragma mark - Override Setter & Getter
- (NSMutableArray *)linkParsers
{
... ...
... ... @@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
340F156D1C310C390002328B /* UINavigation+Pop.m in Sources */ = {isa = PBXBuildFile; fileRef = 340F156C1C310C390002328B /* UINavigation+Pop.m */; };
500053DE1AFB164500AF7D48 /* untitled.html in Resources */ = {isa = PBXBuildFile; fileRef = 500053DD1AFB164500AF7D48 /* untitled.html */; };
50386F0E1B6F3E43000F62A8 /* YHURLCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 50386EA61B6F3E43000F62A8 /* YHURLCache.m */; };
50386F0F1B6F3E43000F62A8 /* config.xml in Resources */ = {isa = PBXBuildFile; fileRef = 50386EA71B6F3E43000F62A8 /* config.xml */; };
... ... @@ -81,6 +82,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
340F156B1C310C390002328B /* UINavigation+Pop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UINavigation+Pop.h"; sourceTree = "<group>"; };
340F156C1C310C390002328B /* UINavigation+Pop.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UINavigation+Pop.m"; sourceTree = "<group>"; };
500053DD1AFB164500AF7D48 /* untitled.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = untitled.html; sourceTree = "<group>"; };
50386EA51B6F3E43000F62A8 /* YHURLCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHURLCache.h; sourceTree = "<group>"; };
50386EA61B6F3E43000F62A8 /* YHURLCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YHURLCache.m; sourceTree = "<group>"; };
... ... @@ -218,9 +221,19 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
340F156A1C310C390002328B /* UINavigation+Pop */ = {
isa = PBXGroup;
children = (
340F156B1C310C390002328B /* UINavigation+Pop.h */,
340F156C1C310C390002328B /* UINavigation+Pop.m */,
);
path = "UINavigation+Pop";
sourceTree = "<group>";
};
50386EA11B6F3E43000F62A8 /* YHExplorer */ = {
isa = PBXGroup;
children = (
340F156A1C310C390002328B /* UINavigation+Pop */,
50386EA21B6F3E43000F62A8 /* Cache */,
50386EA71B6F3E43000F62A8 /* config.xml */,
50386EA81B6F3E43000F62A8 /* cordova.js */,
... ... @@ -652,6 +665,7 @@
files = (
50386F321B6F3E43000F62A8 /* YHNativeMotion.m in Sources */,
50386F171B6F3E43000F62A8 /* CDVJSON.m in Sources */,
340F156D1C310C390002328B /* UINavigation+Pop.m in Sources */,
50386F241B6F3E43000F62A8 /* NSMutableArray+QueueAdditions.m in Sources */,
50386F361B6F3E43000F62A8 /* YHExplorerView.m in Sources */,
50386F261B6F3E43000F62A8 /* CDVYHInterface.m in Sources */,
... ...
... ... @@ -10,13 +10,29 @@
#import "YHExplorerView.h"
@interface WebViewController ()
<YHExplorerDelegate>
<YHExplorerDelegate,UINavigationControllerShouldPop>
@property (weak, nonatomic) IBOutlet YHExplorerView *webView;
@property (strong, nonatomic) UITextView *tv;
@end
@implementation WebViewController
- (BOOL)navigationControllerShouldPop:(UINavigationController *)navigationController
{
// NSLog(@"%@ 点击\n%s", [self class], __func__);
[self.webView removeProgress];
return YES;
}
- (BOOL)navigationControllerShouldStartInteractivePopGestureRecognizer:(UINavigationController *)navigationController
{
// NSLog(@"%@ 手势\n%s", [self class], __func__);
[self.webView removeProgress];
return YES;
}
- (void)dealloc
{
... ...