首页 > 代码库 > UIControl类控件统一管理

UIControl类控件统一管理

static char OperationKey;

 

+(NSString *)eventName:(UIControlEvents)event

{

    switch (event) {

        case UIControlEventTouchDown:          return @"UIControlEventTouchDown";

        case UIControlEventTouchDownRepeat:    return @"UIControlEventTouchDownRepeat";

        case UIControlEventTouchDragInside:    return @"UIControlEventTouchDragInside";

        case UIControlEventTouchDragOutside:   return @"UIControlEventTouchDragOutside";

        case UIControlEventTouchDragEnter:     return @"UIControlEventTouchDragEnter";

        case UIControlEventTouchDragExit:      return @"UIControlEventTouchDragExit";

        case UIControlEventTouchUpInside:      return @"UIControlEventTouchUpInside";

        case UIControlEventTouchUpOutside:     return @"UIControlEventTouchUpOutside";

        case UIControlEventTouchCancel:        return @"UIControlEventTouchCancel";

        case UIControlEventValueChanged:       return @"UIControlEventValueChanged";

        case UIControlEventEditingDidBegin:    return @"UIControlEventEditingDidBegin";

        case UIControlEventEditingChanged:     return @"UIControlEventEditingChanged";

        case UIControlEventEditingDidEnd:      return @"UIControlEventEditingDidEnd";

        case UIControlEventEditingDidEndOnExit:return @"UIControlEventEditingDidEndOnExit";

        case UIControlEventAllTouchEvents:     return @"UIControlEventAllTouchEvents";

        case UIControlEventAllEditingEvents:   return @"UIControlEventAllEditingEvents";

        case UIControlEventApplicationReserved:return @"UIControlEventApplicationReserved";

        case UIControlEventSystemReserved:     return @"UIControlEventSystemReserved";

        case UIControlEventAllEvents:          return @"UIControlEventAllEvents";

        default:

            return @"description";

    }

    return @"description";

}

 

- (void)handleControlEvent:(UIControlEvents)event withBlock:(void(^)(id sender))block {

    

    NSString *methodName = [UIControl eventName:event];

    

    NSMutableDictionary *opreations = (NSMutableDictionary*)objc_getAssociatedObject(self, &OperationKey);

    

    if(opreations == nil)

    {

        opreations = [[NSMutableDictionary alloc] init];

        objc_setAssociatedObject(self, &OperationKey, opreations, OBJC_ASSOCIATION_RETAIN);

    }

    [opreations setObject:block forKey:methodName];

    

    [self addTarget:self action:NSSelectorFromString(methodName) forControlEvents:event];

    

}

 

- (void)removeHandlerForEvent:(UIControlEvents)event

{

    

    NSString *methodName = [UIControl eventName:event];

    NSMutableDictionary *opreations = (NSMutableDictionary*)objc_getAssociatedObject(self, &OperationKey);

    

    if(opreations == nil)

    {

        opreations = [[NSMutableDictionary alloc] init];

        objc_setAssociatedObject(self, &OperationKey, opreations, OBJC_ASSOCIATION_RETAIN);

    }

    [opreations removeObjectForKey:methodName];

    [self removeTarget:self action:NSSelectorFromString(methodName) forControlEvents:event];

}

UIControl类控件统一管理