利用RunTime Hook 实现iOS防止按钮连续响应点击

虽然iOS很少出现这种问题, 而且也有很多办法可以解决.


为了更好的熟悉了解RunTime, 那我们就来看看用RunTime是这么解决的吧.


链接:https://github.com/ConnorForGIT/JCButton


下面是部分代码.


    
static dispatch_once_t onceToken;


    dispatch_once(&onceToken, ^{


        SEL selA = @selector(sendAction:to:forEvent:);
        SEL selB = @selector(mySendAction:to:forEvent:);
        Method methodA =   class_getInstanceMethod(self,selA);
        Method methodB = class_getInstanceMethod(self, selB);
        BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));


        if (isAdd) {


            class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
            
        }else{
            
            method_exchangeImplementations(methodA, methodB);
        }
    });

你可能感兴趣的:(UIButton,runtime,防止连续响应点击)