MRLogicInjection 1.0.0

MRLogicInjection 1.0.0

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最后发布2018年4月

ImageView维护。



  • 作者:
  • stonedong

MRLogicInjection

CI Status Version License Platform

介绍

类似于KVO实现原理,构建AOP(切面编程范式)中逻辑注入的基础组件库。核心机制复杂,但是代码简单。主要依赖于isa-swizzing和method-swizzing技术。该库主要针对instance进行业务逻辑注入,只对一个内存实例生效,而不是整个类。因此,具有场景化的特点,不会造成类污染。只需在需要特定场景中的特定实例上使用该库即可。

安装

MRLogicInjection可以通过CocoaPods安装。要安装它,只需将以下行添加到您的Podfile中:

pod "MRLogicInjection"

功能点

  1. AOP对类实例进行业务逻辑注入
  2. 支持嵌套业务逻辑注入

计划中

  1. 注入的类中的property属性,自动使用辅助变量存储。

使用例子

参照使用该库解决延迟点击的问题:DZDeneyRepeat

运行示例项目,首先克隆仓库,然后从示例目录运行 pod install

选取合适的注入点进行业务逻辑编制

在解决重复点击的问题中,使用

- (void) sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

为注入点,因为所有的UIControl的事件响应都经由此处。

围绕该注入点构建DZDeneyRepeatInjection类,该类包含了关于重复点击问题的所有相关逻辑。



@interface DZDeneyRepeatInjection : UIControl
@property (nonatomic, assign) float denyRepeatTime;
@end


static void* kDZDeneyRepeatTimeKey = &kDZDeneyRepeatTimeKey;
@implementation DZDeneyRepeatInjection

- (void) setDenyRepeatTime:(float)denyRepeatTime
{
    objc_setAssociatedObject(self, kDZDeneyRepeatTimeKey, @(denyRepeatTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (float) denyRepeatTime
{
    NSNumber* num = objc_getAssociatedObject(self, kDZDeneyRepeatTimeKey);
    if (!num) {
        return 0.0f;
    }
    return [num floatValue];
}
- (void) sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    [super sendAction:action to:target forEvent:event];
    if (self.denyRepeatTime > 0.00001) {
        self.userInteractionEnabled = NO;
        __weak typeof(self) weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.denyRepeatTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.userInteractionEnabled = YES;
        });
    }
}
@end

然后使用MRLogicInjection构建业务逻辑注入的辅助方法:


DZDeneyRepeatInjection* DZInjectionDeneyRepeatLogic(UIControl* object, float deneyTime)
{
    DZDeneyRepeatInjection* deney = MRExtendInstanceLogic(object, @[DZDeneyRepeatInjection.class]);
    if ([deney respondsToSelector:@selector(setDenyRepeatTime:)]) {
        [deney setDenyRepeatTime:deneyTime];
    }
    return deney;
}

在特定的页面中对需要防止重复点击的UIControl类进行注入:

DZInjectionDeneyRepeatLogic(anButton, 10);

之后这个实例anButton就具有了防止重复点击的能力,而不影响该类的其他实例。

作者

stonedong, [email protected]

许可证

MRLogicInjection在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。