Skip to content

Commit

Permalink
Merge pull request #894 from didi/feature/ios-mc
Browse files Browse the repository at this point in the history
自定义 手势
  • Loading branch information
WangLao100 authored Aug 25, 2021
2 parents 7956246 + 6a1edff commit 7cd549f
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import <Foundation/Foundation.h>
#import "DoraemonMCMessagePackager.h"

#import "DoraemonMCEventHandler.h"
NS_ASSUME_NONNULL_BEGIN

@interface DoraemonMCCommandExcutor : NSObject
Expand All @@ -17,6 +17,10 @@ NS_ASSUME_NONNULL_BEGIN

+ (void)excuteMessage:(DoraemonMCMessage *)message;


//增加自定义事件
+ (void)addCustomMessage:(NSString *)type eventHandlerName:(DoraemonMCEventHandler *)eventHandler;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#import "DoraemonMCCommandExcutor.h"
#import "DoraemonMCEventHandler.h"

static NSMutableDictionary *eventHandlerMap = nil;
static NSMutableDictionary *externalEventHandlerMap = nil;

@implementation DoraemonMCCommandExcutor

+ (void)excuteMessageStrFromNet:(NSString *)message {
Expand All @@ -17,7 +20,6 @@ + (void)excuteMessageStrFromNet:(NSString *)message {
}

+ (void)excuteMessage:(DoraemonMCMessage *)message {
static NSDictionary *eventHandlerMap = nil ;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
DoraemonMCReuseCellEventHandler *handlerReuseCell = [DoraemonMCReuseCellEventHandler new];
Expand All @@ -29,6 +31,7 @@ + (void)excuteMessage:(DoraemonMCMessage *)message {
@(DoraemonMCMessageTypeTextInput) : [DoraemonMCTextFiledEventHandler new],
@(DoraemonMCMessageTypeTarbarSelected) : [DoraemonMCTabbarEventHandler new]
};
externalEventHandlerMap = [NSMutableDictionary new];
});

[eventHandlerMap enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull typeNumber, DoraemonMCEventHandler * _Nonnull eventHandler, BOOL * _Nonnull stop) {
Expand All @@ -38,6 +41,34 @@ + (void)excuteMessage:(DoraemonMCMessage *)message {
}
}];


[externalEventHandlerMap enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull typeString, DoraemonMCEventHandler * _Nonnull eventHandler, BOOL * _Nonnull stop) {
if ([message.customType isEqualToString:typeString]) {
[eventHandler handleEvent:message];
*stop = YES;
}
}];


}

//增加自定义事件
+ (void)addCustomMessage:(NSString *)type eventHandlerName:(DoraemonMCEventHandler *)eventHandler {
if (eventHandler && type) {

[externalEventHandlerMap enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull typeString, DoraemonMCEventHandler * _Nonnull eventHandler, BOOL * _Nonnull stop) {
if ([type isEqualToString:typeString]) {
*stop = YES;
NSAssert(stop, @"重复添加事件");
}
}];

[externalEventHandlerMap setValue:eventHandler forKey:type];
}
}





@end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
indexPath:(NSIndexPath *)indexPath
messageType:(DoraemonMCMessageType)type;

+ (DoraemonMCMessage *)sendCustomMessageWithView:(UIView *)view
eventInfo:(NSDictionary *)eventInfo
messageType:(NSString *)type;



@end


Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,18 @@ + (void)sendMessageWithView:(UIView *)view
}

}


+ (DoraemonMCMessage *)sendCustomMessageWithView:(UIView *)view
eventInfo:(NSDictionary *)eventInfo
messageType:(NSString *)type {

DoraemonMCMessage *message = [DoraemonMCMessagePackager packageCustomMessageWithView:view
eventInfo:eventInfo
messageType:type];

[DoraemonMCServer sendMessage:message.toMessageString];
return message;
}
@end

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ NS_ASSUME_NONNULL_BEGIN

- (BOOL)handleEvent:(DoraemonMCMessage*)eventInfo;

- (UIView *)fetchTargetView;

@end

@interface DoraemonMCGestureRecognizerEventHandler : DoraemonMCEventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ typedef NS_ENUM(NSInteger , DoraemonMCMessageType) {

// 消息类型
@property (nonatomic , assign) DoraemonMCMessageType type;

// 自定义消息类型
@property (nonatomic , assign) NSString * customType;
// 控件的xPath
@property (nonatomic , copy ) NSString *xPath;

Expand All @@ -54,6 +55,13 @@ typedef NS_ENUM(NSInteger , DoraemonMCMessageType) {
indexPath:(NSIndexPath *)indexPath
messageType:(DoraemonMCMessageType)type;

/**
自定义事件
*/
+ (DoraemonMCMessage *)packageCustomMessageWithView:(UIView *)view
eventInfo:(NSDictionary *)eventInfo
messageType:(NSString *)type;

/***
根据从网络上获取的消息字符串, 解析出消息对象
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
static NSString const *kEventInfoKey = @"eventInfo";
static NSString const *kXpathKey = @"xPath";
static NSString const *kTypeKey = @"type";

static NSString const *kcustomTypeKey =@"customType";
@implementation DoraemonMCMessagePackager

/**
Expand Down Expand Up @@ -100,13 +100,33 @@ + (DoraemonMCMessage *)packageMessageWithView:(UIView *)view
}


/*
* 自定义事件
*/
+ (DoraemonMCMessage *)packageCustomMessageWithView:(UIView *)view
eventInfo:(NSDictionary *)eventInfo
messageType:(NSString *)type {
DoraemonMCMessage *messageInstance = [[DoraemonMCMessage alloc] init];
messageInstance.customType = type;
messageInstance.xPath = [DoraemonMCXPathSerializer xPathStringWithView:view];
messageInstance.eventInfo = eventInfo;
messageInstance.isFirstResponder = view.isFirstResponder;
UIViewController *vc = [DoraemonMCXPathSerializer ownerVCWithView:view];
if (vc) {
messageInstance.currentVCClassName = NSStringFromClass(vc.class) ;
}

return messageInstance;

}
/***
根据从网络上获取的消息字符串, 解析出消息对象
*/
+ (DoraemonMCMessage *)parseMessageString:(NSString *)messageString {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[messageString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:NULL];
DoraemonMCMessage *messageInstance = [[DoraemonMCMessage alloc] init];
messageInstance.type = [dict[kTypeKey] integerValue];
messageInstance.customType = dict[kcustomTypeKey];
messageInstance.xPath = dict[kXpathKey];
messageInstance.eventInfo = dict[kEventInfoKey];
messageInstance.currentVCClassName = dict[kVcClsNameKey];
Expand All @@ -119,13 +139,23 @@ + (DoraemonMCMessage *)parseMessageString:(NSString *)messageString {
@implementation DoraemonMCMessage

- (NSString *)toMessageString {

NSDictionary *dict = @{
kTypeKey : @(self.type),
kXpathKey : self.xPath?:@"",
kEventInfoKey : self.eventInfo?:@{},
kVcClsNameKey: self.currentVCClassName?:@"",
kIsFirstResponderKey : @(self.isFirstResponder)
};
if (self.customType.length) {
dict = @{
kcustomTypeKey : self.customType,
kXpathKey : self.xPath?:@"",
kEventInfoKey : self.eventInfo?:@{},
kVcClsNameKey: self.currentVCClassName?:@"",
kIsFirstResponderKey : @(self.isFirstResponder)
};
}
if ([NSJSONSerialization isValidJSONObject:dict]) {
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:NULL] encoding:NSUTF8StringEncoding];
}
Expand Down
10 changes: 8 additions & 2 deletions iOS/DoraemonKitDemo/DoraemonKitDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0977CCE0269F394100A6463E /* DoraemonDemoMultiControlViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0977CCDF269F394000A6463E /* DoraemonDemoMultiControlViewController.m */; };
097F6DA326D53E5D00841403 /* DoraemonDemoMultiSlideView.m in Sources */ = {isa = PBXBuildFile; fileRef = 097F6DA226D53E5D00841403 /* DoraemonDemoMultiSlideView.m */; };
09B042C526A8585500045D2A /* DoraemonDemoMultiConLongPressGesture.m in Sources */ = {isa = PBXBuildFile; fileRef = 09B042C426A8585500045D2A /* DoraemonDemoMultiConLongPressGesture.m */; };
09B042C926A860B400045D2A /* DoraemonDemoMultiConPinchGesture.m in Sources */ = {isa = PBXBuildFile; fileRef = 09B042C826A860B400045D2A /* DoraemonDemoMultiConPinchGesture.m */; };
09B042CD26A8760E00045D2A /* DoraemonDemoMultiConRotationGesture.m in Sources */ = {isa = PBXBuildFile; fileRef = 09B042CC26A8760E00045D2A /* DoraemonDemoMultiConRotationGesture.m */; };
Expand Down Expand Up @@ -57,6 +58,8 @@
/* Begin PBXFileReference section */
0977CCDE269F394000A6463E /* DoraemonDemoMultiControlViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoraemonDemoMultiControlViewController.h; sourceTree = "<group>"; };
0977CCDF269F394000A6463E /* DoraemonDemoMultiControlViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DoraemonDemoMultiControlViewController.m; sourceTree = "<group>"; };
097F6DA126D53E5D00841403 /* DoraemonDemoMultiSlideView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoraemonDemoMultiSlideView.h; sourceTree = "<group>"; };
097F6DA226D53E5D00841403 /* DoraemonDemoMultiSlideView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DoraemonDemoMultiSlideView.m; sourceTree = "<group>"; };
09B042C326A8585500045D2A /* DoraemonDemoMultiConLongPressGesture.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoraemonDemoMultiConLongPressGesture.h; sourceTree = "<group>"; };
09B042C426A8585500045D2A /* DoraemonDemoMultiConLongPressGesture.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DoraemonDemoMultiConLongPressGesture.m; sourceTree = "<group>"; };
09B042C726A860B400045D2A /* DoraemonDemoMultiConPinchGesture.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoraemonDemoMultiConPinchGesture.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -172,6 +175,8 @@
09B042D426A877ED00045D2A /* DoraemonDemoMultiConScreenEdgePanGesture.m */,
09B042D726A8781A00045D2A /* DoraemonDemoMultiConTapGesture.h */,
09B042D826A8781A00045D2A /* DoraemonDemoMultiConTapGesture.m */,
097F6DA126D53E5D00841403 /* DoraemonDemoMultiSlideView.h */,
097F6DA226D53E5D00841403 /* DoraemonDemoMultiSlideView.m */,
);
path = MultiControl;
sourceTree = "<group>";
Expand Down Expand Up @@ -597,6 +602,7 @@
DAE7BFAD20205C20008D49D8 /* NSObject+Runtime.m in Sources */,
DABBE3B521D3AA4D0070518E /* DoraemonWKWebViewViewController.m in Sources */,
0977CCE0269F394100A6463E /* DoraemonDemoMultiControlViewController.m in Sources */,
097F6DA326D53E5D00841403 /* DoraemonDemoMultiSlideView.m in Sources */,
DAFE052D21BD4A4D00F97A59 /* DoraemonDemoMockGPSAnnotation.m in Sources */,
DAFE052921BD4A4D00F97A59 /* DoraemonDemoCrashViewController.m in Sources */,
DA0C6F3A1FDEBE3800F43588 /* TestPlugin.m in Sources */,
Expand Down Expand Up @@ -771,14 +777,14 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = 46A29A7ADN;
DEVELOPMENT_TEAM = 7M2BQXS6D5;
ENABLE_BITCODE = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "DoraemonKitDemo/DoraemonKitDemo-PrefixHeader.pch";
INFOPLIST_FILE = DoraemonKitDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.didi.dokit.demo.test.a;
PRODUCT_BUNDLE_IDENTIFIER = com.didi.dokit.demo.test.a123;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
Loading

0 comments on commit 7cd549f

Please sign in to comment.