重写方法,原方法也执行

#import "TTTAttributedLabel.h"

@interface TTTAttributedLabel (Action)

/**
 UILongPressGestureRecognizer action.
 
 @param sender  The UILongPressGestureRecognizer.
 
 */
- (void)longPressGestureDidFire:(UILongPressGestureRecognizer *)sender;

/**
 *  The current activeLink.
 */
@property (nonatomic, strong) TTTAttributedLabelLink *activeLink;

@end


#import "TTTAttributedLabel+Action.h"

@implementation TTTAttributedLabel (Action)
static char activeLinkKey;

- (void)longPressGestureDidFire:(UILongPressGestureRecognizer *)sender {
    
    Class currentClass = [TTTAttributedLabel class];
    if (currentClass) {
        unsigned int methodCount;
        Method *methodList = class_copyMethodList(currentClass, &methodCount);
        IMP lastImp = NULL;
        SEL lastSel = NULL;
        for (NSInteger i = 0; i < methodCount; i++) {
            Method method = methodList[i];
            NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(method)) encoding:NSUTF8StringEncoding];
            if ([@"longPressGestureDidFire:" isEqualToString:methodName]) {
                lastImp = method_getImplementation(method);
                lastSel = method_getName(method);
            }
        }
        typedef void (*fn)(id, SEL, id);
        if (lastImp != NULL) {
            fn f = (fn)lastImp;
            f(self, lastSel, sender);
        }
        free(methodList);
    }
}


- (void)setActiveLink:(TTTAttributedLabelLink *)activeLink {
    objc_setAssociatedObject(self, &activeLinkKey, activeLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (TTTAttributedLabelLink *)activeLink {
    return  objc_getAssociatedObject(self, &activeLinkKey);
}

@end

你可能感兴趣的:(重写方法,原方法也执行)