美文网首页
重写方法,原方法也执行

重写方法,原方法也执行

作者: 颜子 | 来源:发表于2018-03-19 18:21 被阅读11次
    #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
    
    

    相关文章

      网友评论

          本文标题:重写方法,原方法也执行

          本文链接:https://www.haomeiwen.com/subject/exgpqftx.html